1.双向多对多:B(Teacher)有多个A(Student),A也有多个B
注意要在一方加上inverse属性,否则一起维护关联关系可能会造成主键冲突:
package roadArchitectWeb;
- import java.util.HashSet;
- import java.util.Set;
- public class Teacher {
- private Integer Id;
- private String Name;
- private String PhoneNum;
- private Set<Student> Students = new HashSet<>();
- public Set<Student> getStudents() {
- return Students;
- }
- public void setStudents(Set<Student> students) {
- Students = students;
- }
- public Integer getId() {
- return Id;
- }
- public void setId(Integer id) {
- Id = id;
- }
- public String getName() {
- return Name;
- }
- public void setName(String name) {
- Name = name;
- }
- public String getPhoneNum() {
- return PhoneNum;
- }
- public void setPhoneNum(String phoneNum) {
- PhoneNum = phoneNum;
- }
- public Teacher() {
- }
- @Override
- public String toString() {
- return "Teacher [Id=" + Id + ", Name=" + Name + ", PhoneNum=" + PhoneNum + ", Students=" + Students + "]";
- }
- }
- package roadArchitectWeb;
- import java.util.HashSet;
- import java.util.Set;
- public class Student {
- private Integer Id;
- private String sName;
- private String Age;
- private Set<Teacher> Teachers = new HashSet<>();
- public Set<Teacher> getTeachers() {
- return Teachers;
- }
- public void setTeachers(Set<Teacher> teachers) {
- Teachers = teachers;
- }
- public Integer getId() {
- return Id;
- }
- public void setId(Integer id) {
- Id = id;
- }
- public String getsName() {
- return sName;
- }
- public void setsName(String sName) {
- this.sName = sName;
- }
- public String getAge() {
- return Age;
- }
- public void setAge(String age) {
- Age = age;
- }
- @Override
- public String toString() {
- return "Student [Id=" + Id + ", sName=" + sName + ", Age=" + Age + ", Teachers=" + Teachers + "]";
- }
- public Student(){
- }
- }
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <!-- Generated 2016-4-21 19:12:13 by Hibernate Tools 3.5.0.Final -->
- <hibernate-mapping package="roadArchitectWeb">
- <class name="Student" table="STUDENT">
- <id name="Id" type="java.lang.Integer">
- <column name="ID" />
- <generator class="native">
- </generator>
- </id>
- <property name="sName" type="java.lang.String">
- <column name="SNAME" />
- </property>
- <property name="Age" type="java.lang.String">
- <column name="AGE" />
- </property>
- <set name="Teachers" table="teacher_student" inverse="true">
- <key column="studentId"></key>
- <many-to-many column="teacherId" class="Teacher"></many-to-many>
- </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">
- <!-- Generated 2016-4-21 19:12:13 by Hibernate Tools 3.5.0.Final -->
- <hibernate-mapping package="roadArchitectWeb">
- <class name="Student" table="STUDENT">
- <id name="Id" type="java.lang.Integer">
- <column name="ID" />
- <generator class="native">
- </generator>
- </id>
- <property name="sName" type="java.lang.String">
- <column name="SNAME" />
- </property>
- <property name="Age" type="java.lang.String">
- <column name="AGE" />
- </property>
- <set name="Teachers" table="teacher_student" inverse="true">
- <key column="studentId"></key>
- <many-to-many column="teacherId" class="Teacher"></many-to-many>
- </set>
- </class>
- </hibernate-mapping>
- package roadArchitectWeb;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- import org.junit.Test;
- import com.mysql.jdbc.interceptors.ServerStatusDiffInterceptor;
- public class HibernateTest {
- @Test
- public void test() {
- SessionFactory sessionFactory = null;
- Configuration configuration = new Configuration().configure();
- sessionFactory = configuration.buildSessionFactory();
- Session session = sessionFactory.openSession();
- Transaction transaction = session.beginTransaction();
- /*begin*/
- Teacher teacher = new Teacher();
- Student student = new Student();
- teacher.setName("chenT");
- teacher.setPhoneNum("18788837117");
- student.setsName("ma");
- student.setAge("11");
- student.getTeachers().add(teacher);
- teacher.getStudents().add(student);
- session.save(student);
- session.save(teacher);
- // student = (Student)session.get(Student.class, 1);
- // teacher = (Teacher)session.get(Teacher.class,1);
- // System.out.println("HibernateTest.test()S:"+student.getTeacher().getName());
- // System.out.println("HibernateTest.test()T:"+teacher.getStudents().size());
- transaction.commit();
- session.close();
- sessionFactory.close();
- }
- }
这个时候的双向多对多,数据库会生成一个关联表,里面有且只有两个类的外键。
2.多对多拆分两个一对多
针对上面那个情况,因为自动生成的关联表中只有两个外键这两个列,如果我想在这个列种存放其他属性怎么办? 因为关联表是自动生成的,这个时候的解决办法就是把多对多的关联关系改为两个一对多双向关联。 举个例子:
一个订单有多种商品,种个商品可能在多个订单里。但是我如果描述一个订单里一种商品的数量该怎么描述,必须建立一个订单-商品关联表,然后在关联表中设置商品的数量,这样一条记录就代表一个订单,一种商品和这个商品的数量。
那应该如何实现呢? 把订单当作一,把商品当作一,把关系表(这个关系表与订单以及商品一样是通过一个类生成的,而不是hibernate自动生成)当作多,并且建立两个一对多的双向关联关系,就可以实现。
代码实现按照之前的一对多双向示例即可。