本文主要是介绍Hibernate学习总结:ManyToMany单向关联,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
说明:
一、一个老师可以教许多学生,并且一个学生可以有许多老师。
二、本次使用的Hibernate版本是hibernate-release-4.3.11.Final.
先写Annotation版本:
实体类:
Teacher.java:package com.buaa.hibernate.bean;import java.util.HashSet;
import java.util.Set;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;@Entity
public class Teacher {private int id;private String name;private Set<Student> students = new HashSet<Student>();@ManyToMany@JoinTable(name="t_s",joinColumns={@JoinColumn(name="teacher_id")},inverseJoinColumns={@JoinColumn(name="student_id")})public Set<Student> getStudents() {return students;}public void setStudents(Set<Student> students) {this.students = students;}@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
Student.java:
package com.buaa.hibernate.bean;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;@Entity
public class Student {private int id;private String name;@Id@GeneratedValuepublic int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
Xml版本:
Teacher.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN""http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.buaa.hibernate.bean"><class name="Teacher"><id name="id"><generator class="native" /></id><property name="name"/><set name="students" table="t_s"><key column="teacher_id"></key><many-to-many class="Student" column="student_id" /></set></class><class name="Student"><id name="id"><generator class="native" /></id><property name="name"/></class></hibernate-mapping>
此处将两个类写在一个配置文件里了。
下面是Hibernate配置文件:hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory><property name="connection.driver_class">com.mysql.jdbc.Driver</property><!-- Assume test is the database name --><property name="connection.url">jdbc:mysql://localhost/test</property><property name="connection.username">root</property><property name="connection.password">123456</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="show_sql">true</property><property name="format_sql">true</property><!-- <property name="hbm2ddl.auto">create</property> --><!-- <mapping resource="com/buaa/hibernate/bean/Teacher.hbm.xml"/>--><mapping class="com.buaa.hibernate.bean.Student"/><mapping class="com.buaa.hibernate.bean.Teacher"/><!-- --></session-factory>
</hibernate-configuration>
最后写Junit测试类:
ManyToManyTest.java:
package com.buaa.hibernate.bean;import java.util.HashSet;
import java.util.Set;import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;public class ManyToManyTest {@Testpublic void test(){Student user1 = new Student();user1.setName("u1");Student user2 = new Student();user2.setName("u2");Set<Student> students = new HashSet<Student>();students.add(user1);students.add(user2);Teacher group = new Teacher();group.setName("A");group.setStudents(students);Configuration configuration = new Configuration().configure();ServiceRegistry service = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();SessionFactory factory = configuration.buildSessionFactory(service);Session session = factory.openSession();session.beginTransaction();session.save(user1);session.save(user2);session.save(group);session.getTransaction().commit();session.close();factory.close();}@Testpublic void SchemaExportTest(){Configuration cfg = new Configuration().configure();//第一个参数是否将sql语句输出到控制台,第二个参数是否在数据库中生成对应的表格new SchemaExport(cfg).create(true,true);}
}
这篇关于Hibernate学习总结:ManyToMany单向关联的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!