Hibernate 再接触 多对多单向双向关联
情景:一个老师可能有多个学生,一个学生也可能有多个老师
多对一单向:
例如老师知道自己教哪些学生,学生却不知道自己被哪些老师教
方法:使用第三张表 分别存两张表的id
annotation
Student.java
package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity public class Student { private int id; private String name; @Id @GeneratedValue public 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; } }
Teacher.java
package com.bjsxt.hibernate; 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; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity public class Teacher { private int id; private String name; private Set<Student> students = new HashSet<Student>(); @Id @GeneratedValue public 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; } @ManyToMany @JoinTable(name="t_s", joinColumns={@JoinColumn(name="teacher_id")}, //自己这个类的外键id inverseJoinColumns={@JoinColumn(name="student_id")} //对方那张表的外键id ) public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } }
XML
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.bjsxt.hibernate.Student"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.bjsxt.hibernate.Teacher"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <set name="students" table="t_s"> <key column="teacher_id"></key> <many-to-many class="com.bjsxt.hibernate.Student" column="student_id"/> </set> </class> </hibernate-mapping>
test
@Test public void testSchemaExport() { new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); }
多对多 双向关联 必设mapped
老师知道自己教那些学生 学生知道被哪些老师教
ANNOTATION
Student.java
package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Student { private int id; private String name; private Set<Teacher> teachers = new HashSet<Teacher>(); @ManyToMany(mappedBy="students") public Set<Teacher> getTeachers() { return teachers; } public void setTeachers(Set<Teacher> teachers) { this.teachers = teachers; } @Id @GeneratedValue public 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; } }
Teacher.java
package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToMany; @Entity public class Student { private int id; private String name; private Set<Teacher> teachers = new HashSet<Teacher>(); @ManyToMany(mappedBy="students") public Set<Teacher> getTeachers() { return teachers; } public void setTeachers(Set<Teacher> teachers) { this.teachers = teachers; } @Id @GeneratedValue public 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
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.bjsxt.hibernate.Student"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <set name="teachers" table="t_s"> <key column="student_id"></key> <many-to-many class="com.bjsxt.hibernate.Teacher" column="teacher_id"/> </set> </class> </hibernate-mapping>
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.bjsxt.hibernate.Teacher"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <set name="students" table="t_s"> <key column="teacher_id"></key> <many-to-many class="com.bjsxt.hibernate.Student" column="student_id"/> </set> </class> </hibernate-mapping>
test
@Test public void testSchemaExport() { new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); }