Hibernate关联关系之多对多

来源:互联网 发布:全新0信誉淘宝店铺 编辑:程序博客网 时间:2024/05/16 05:37

测试学生和教师多对多关系:

注意:由于多对多关系,你中有我,我中有你,那么会造成在操作持久化状态对象的时候,两边都会去操作,从而导致进行数据库的重复更新操作。所以要在一端的set标签中配置inverse="true"

Teacher:

package com.xxc.domain;import java.util.HashSet;import java.util.Set;public class Teacher {private Integer id;private String teaNo;//教师类中有学生集合private Set<Student> stus = new HashSet<Student>();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTeaNo() {return teaNo;}public void setTeaNo(String teaNo) {this.teaNo = teaNo;}public Set<Student> getStus() {return stus;}public void setStus(Set<Student> stus) {this.stus = stus;}//定义增加学生对象至集合的方法public void addStudent(Student...stus){for(Student s : stus){this.stus.add(s);}}}

Teacher.hbm.xml:

<!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.xxc.domain.Teacher" table="teas" lazy="false"><id name="id" column="id" type="integer"><generator class="identity"></generator></id><property name="teaNo" column="teaNo" type="string" length="20"/><!-- table="link"表示中间表的名字   因为是多对多关联,你中有我,我中有你,那势必在session缓存中会发生两端集合中都进行了改变后,会重复更新数据库操作 所以要配置inverse="true"  只要在一端配置就行,Teacher和Student类都可--><set name="stus" table="link" inverse="true"><!-- 这个表示在中间表中此类的字段名 --><key column="tid"/><many-to-many class="com.xxc.domain.Student" column="sid"/></set></class></hibernate-mapping>


Student:

package com.xxc.domain;import java.util.HashSet;import java.util.Set;public class Student {private Integer id;private String stuNo;//学生类中有教师集合private Set<Teacher> teas = new HashSet<Teacher>();public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getStuNo() {return stuNo;}public void setStuNo(String stuNo) {this.stuNo = stuNo;}public Set<Teacher> getTeas() {return teas;}public void setTeas(Set<Teacher> teas) {this.teas = teas;}//定义增加教师对象到教师集合中public void addTeacher(Teacher...teas){for(Teacher t : teas){this.teas.add(t);}}}


Student.hbm.xml:

<!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.xxc.domain.Student" table="stus" lazy="false"><id name="id" column="id" type="integer" ><generator class="identity"></generator></id><property name="stuNo" column="stuNo" type="string" length="20"/><set name="teas" table="link"><key column="sid"/><many-to-many class="com.xxc.domain.Teacher" column="tid"/></set></class></hibernate-mapping>



测试类:

package com.xxc.app;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.BeforeClass;import org.junit.Test;import com.xxc.domain.Student;import com.xxc.domain.Teacher;public class App {private static SessionFactory sf = null;@BeforeClasspublic static void initialize(){Configuration config = new Configuration();/*  也可以写成这样的形式 *sf = config.addClass(Customer.class).addClass(Order.class).buildSessionFactory(); */config.addClass(Teacher.class);config.addClass(Student.class);sf = config.buildSessionFactory();}@Testpublic void insertCustomer(){Session session = sf.openSession();Transaction t = session.beginTransaction();Teacher t1= new Teacher();t1.setTeaNo("11");Teacher t2= new Teacher();t2.setTeaNo("22");Student s1 = new Student();s1.setStuNo("111");Student s2 = new Student();s2.setStuNo("222");Student s3 = new Student();s3.setStuNo("333");Student s4 = new Student();s4.setStuNo("444");t1.addStudent(s1,s2,s3);t2.addStudent(s2,s3,s4);s1.addTeacher(t1);s2.addTeacher(t1,t2);s3.addTeacher(t1,t2);s4.addTeacher(t2);/*t1.getStus().add(s1);t1.getStus().add(s2);t1.getStus().add(s3);s1.getTeas().add(t1);s2.getTeas().add(t1);s2.getTeas().add(t2);s3.getTeas().add(t1);s3.getTeas().add(t2);s4.getTeas().add(t2);t2.getStus().add(s2);t2.getStus().add(s3);t2.getStus().add(s4);*/session.save(t1);session.save(t2);session.save(s1);session.save(s2);session.save(s3);session.save(s4);t.commit();session.close();}}


原创粉丝点击