hibernate的基础学习--多对多关联

Posted 小小暮雨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hibernate的基础学习--多对多关联相关的知识,希望对你有一定的参考价值。

多对多采用学生老师模型进行测试

学生配置文件:

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <!DOCTYPE hibernate-mapping PUBLIC 
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 <!-- 映射配置 -->
 6 <hibernate-mapping>
 7     <class name="hib.po.Student" table="t_student">
 8         <id name="stuId" column="stu_id">
 9             <generator class="uuid"></generator>
10         </id>
11         <property name="stuName" column="stuname"></property>
12         
13         <!--  和老师的关联关系:teachers 
14             name:关联关系属性名称
15             table:中间表名称
16             inverse:关联关系的维护权
17             cascade:级联 delete update
18         -->
19         <set name="teachers" table="t_stu_tea">
20             <!--column:中间表中指向当前模型对应表(t_student)的外键名-->
21             <key column="stu_id"></key>
22             <!-- 
23                 class:关联关系字段集合的元素的类型
24                 column:中间表指向当前模型关联的模型对应表(t_student)的外键
25              -->
26             <many-to-many class="hib.po.Teacher" column="tea_id"></many-to-many>
27         </set>
28     </class>
29 </hibernate-mapping>
View Code

 

老师配置文件:

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <!DOCTYPE hibernate-mapping PUBLIC 
 3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 <!-- 映射配置 -->
 6 <hibernate-mapping>
 7     <class name="hib.po.Teacher" table="t_teacher">
 8         <id name="teaId" column="tea_id">
 9             <generator class="uuid"></generator>
10         </id>
11         <property name="teaName" column="teaname"></property>
12         
13         <!-- 和学生的关联关系:students 
14             name:关联关系属性名称
15             table:中间表名称(必须和对应关联表中配置一致)
16             放弃维护关系 inverse="true"
17         -->
18         <set name="students" table="t_stu_tea">
19             <!--column:中间表中指向当前模型对应表(t_teacher)的外键名-->
20             <key column="tea_id"></key>
21             
22             <!-- 
23                 class:关联关系字段集合的元素的类型
24                 column:中间表指向当前模型关联的模型对应表(t_student)的外键
25              -->
26             <many-to-many class="hib.po.Student" column="stu_id"></many-to-many>
27         </set>
28     </class>
29 </hibernate-mapping>
View Code

 

测试方法:

 1 public class TestHibernate {
 2 
 3     /**
 4      * 添加数据记录
 5      */
 6     @Test
 7     public void addTeacherStudent() {
 8         SessionFactory sf = H3Util.getSessionFactory();
 9         Session session = sf.openSession();
10         Transaction ts = session.beginTransaction();
11         
12         Student stu1 = new Student(null, "张三", null);
13         Student stu2 = new Student(null, "李四", null);
14         
15         Teacher tea1 = new Teacher(null, "张老师", null);
16         Teacher tea2 = new Teacher(null, "李老师", null);
17         
18         
19         session.save(stu1);
20         session.save(stu2);
21         session.save(tea1);
22         session.save(tea2);
23         
24         ts.commit();
25         session.close();
26     }
27     @Test /* 关联关系*/
28     public void updateStudent() {
29         SessionFactory sf = H3Util.getSessionFactory();
30         Session session = sf.openSession();
31         Transaction ts = session.beginTransaction();
32         
33         Teacher t1 = (Teacher) session.get(Teacher.class, "4028810a57d19c510157d19c55650002");
34         Student s1 = (Student) session.get(Student.class, "4028810a57d19c510157d19c554a0000");
35         Student s2 = (Student) session.get(Student.class, "4028810a57d19c510157d19c55650001");
36         Set<Teacher> st1 = s1.getTeachers();
37         st1.add(t1);
38         Set<Teacher> st2 = s2.getTeachers();
39         st2.add(t1);
40         s1.setTeachers(st1);
41         s2.setTeachers(st2);
42         session.update(s1);
43         session.update(s2);
44         
45         ts.commit();
46         session.close();
47     }
48     @Test
49     public void delStudent1() {
50         SessionFactory sf = H3Util.getSessionFactory();
51         Session session = sf.openSession();
52         Transaction ts = session.beginTransaction();
53         
54         //张三
55         Student stu = (Student) session.get(Student.class, "4028810a57d19c510157d19c554a0000");
56         
57         //先删除关联关系(中间表字段)再删除本对象
58         session.delete(stu);
59         
60         ts.commit();
61         session.close();
62     }
63     
64 }

 

以上是关于hibernate的基础学习--多对多关联的主要内容,如果未能解决你的问题,请参考以下文章

Hibernate学习笔记 --- 创建基于中间关联表的多对多映射关系

Hibernate:n-n关联关系

hibernate基础12:关联映射之基于主键的双项多对多

hibernate基础12:关联映射之基于主键的双项多对多

关联映射 ---- Hibernate之多对多关系

java框架hibernate多对多如何进行关联查询