(九)Hibernate的多对多关联关系
一、概述
多对多关联关系在java对象中可以通过定义集合类型来实现关联关系。 在关系数据模型中,无法直接表达表和表之间的多对多关联关系,而是需要创建一个中间表包含了两边的主键,来表达两张表的多对多关联关系。
实例:我们用一个Student和Course(学生和课程)的例子来演示多对多关联关系。
(1)创建Student和Course类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Student { private Integer id; private String name; //用一个集合包含该学生所选的课程对象 private Set<Course> courses= new HashSet<Course>(); public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Set<Course> getCourses() { return courses; } public void setCourses(Set<Course> courses) { this .courses = courses; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public class Course { private Integer id; private String name; //用一个集合包含所有选择该课程的学生 private Set<Student> students= new HashSet<Student>(); public Integer getId() { return id; } public void setId(Integer id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this .students = students; } } |
(2)编写我们的映射文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <hibernate-mapping > < class name= "com.cad.domain.Course" table= "course" > <id name= "id" column= "id" > <generator class = "native" ></generator> </id> <property name= "name" column= "name" ></property> <!--table属性用来指定生成的中间表的表名称 inverse指定关联关系由Student维护--> <set name= "students" table= "student_course" inverse= "true" > <key column= "cid" ></key> <!--<many-to-many>元素中的column属性指定本表通过中间表中的sid外键关联到Student对象--> <many-to-many class = "com.cad.domain.Student" column= "sid" ></many-to-many> </set> </ class > </hibernate-mapping> |
1 2 3 4 5 6 7 8 9 10 11 12 13 | <hibernate-mapping > < class name= "com.cad.domain.Student" table= "student" > <id name= "id" column= "id" > <generator class = "native" ></generator> </id> <property name= "name" column= "name" ></property> <!-进行级联保存和更新操作--> <set name= "courses" table= "student_course" cascade= "save-update" > <key column= "sid" ></key> <many-to-many class = "com.cad.domain.Course" column= "cid" ></many-to-many> </set> </ class > </hibernate-mapping> |
测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | public class Demo { @Test public void fun(){ //读取配置文件 Configuration conf= new Configuration().configure(); //根据配置创建factory SessionFactory sessionfactory=conf.buildSessionFactory(); Session session = sessionfactory.openSession(); Transaction ts=session.beginTransaction(); //创建两个Student Student s1= new Student(); s1.setName( "tom" ); Student s2= new Student(); s2.setName( "jack" ); //创建三个Course Course c1= new Course(); c1.setName( "语文" ); Course c2= new Course(); c2.setName( "数学" ); Course c3= new Course(); c3.setName( "英语" ); //因为设置了关联关系由Student维护,所以不需要课程再来关联Student s1.getCourses().add(c1); s1.getCourses().add(c2); s1.getCourses().add(c3); s2.getCourses().add(c1); s2.getCourses().add(c2); s2.getCourses().add(c3); //由于设置了级联保存,所以只保存Student即可 session.save(s1); session.save(s2); ts.commit(); session.close(); sessionfactory.close(); } } 结果,数据库中生成了三张表。 student和course 还有中间表student_course 里面的数据也相互对应。 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)