多对多(many-to-many):在操作和性能方面都不太理想,所以多对多的映射使用较少,实际使用中最好转换成一对多的对象模型;hibernate会为我们创建中间关联表,转换成两个一对多。
1. E-R图
2. 实体类:
Teacher实体类如下:
- package com.reiyen.hibernate.domain;
- import java.util.Set;
- public class Teacher {
- private int id;
- private String name;
- private Set<Student> students;
- //setter和getter方法
- }
package com.reiyen.hibernate.domain; import java.util.Set; public class Teacher { private int id; private String name; private Set<Student> students; //setter和getter方法 }
Student实体类如下:
- package com.reiyen.hibernate.domain;
- import java.util.Set;
- public class Student {
- private int id;
- private String name;
- private Set<Teacher> teachers;
- //setter和getter方法
- }
package com.reiyen.hibernate.domain; import java.util.Set; public class Student { private int id; private String name; private Set<Teacher> teachers; //setter和getter方法 }
3.映射文件如下:
Teacher.hbm.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 package="com.reiyen.hibernate.domain">
- <class name="Teacher">
- <id name="id">
- <generator class="native" />
- </id>
- <property name="name" />
- <!-- 通过table项告诉hibernate中间表的名称 -->
- <set name="students" table="teacher_student">
- <!-- 通过key属性告诉hibernate在中间表里面查询teacher_id值相应的teacher记录 -->
- <key column="teacher_id" />
- <!-- 通过column项告诉hibernate对student表中查找student_id值相就的studnet记录 -->
- <many-to-many class="Student" column="student_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 package="com.reiyen.hibernate.domain"> <class name="Teacher"> <id name="id"> <generator class="native" /> </id> <property name="name" /> <!-- 通过table项告诉hibernate中间表的名称 --> <set name="students" table="teacher_student"> <!-- 通过key属性告诉hibernate在中间表里面查询teacher_id值相应的teacher记录 --> <key column="teacher_id" /> <!-- 通过column项告诉hibernate对student表中查找student_id值相就的studnet记录 --> <many-to-many class="Student" column="student_id" /> </set> </class> </hibernate-mapping>
Student.hbm.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 package="com.reiyen.hibernate.domain">
- <class name="Student" >
- <id name="id" >
- <generator class="native" />
- </id>
- <property name="name" />
- <set name="teachers" table="teacher_student">
- <key column="student_id" />
- <many-to-many class="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 package="com.reiyen.hibernate.domain"> <class name="Student" > <id name="id" > <generator class="native" /> </id> <property name="name" /> <set name="teachers" table="teacher_student"> <key column="student_id" /> <many-to-many class="Teacher" column="teacher_id"/> </set> </class> </hibernate-mapping>
一定要注意映射文件中<many-to-many class="Teacher" column="teacher_id"/>中class的值,它必须与你另一个关联映射文件中的class属性的name值一致,其实就是与你的实体类的类名一致,如:<many-to-many class="Teacher" column="teacher_id"/>中class的值就不能写成"teacher"。如果写成这样的话,就会抛出如下异常:An association from the table teacher_student refers to an unmapped class: com.reiyen .hibernate.domain.teacher
4. 测试程序如下:
- public class Many2Many {
- public static void main(String[] args) {
- add();
- //query(1);
- }
- static void query(int id) {
- Session s = null;
- Transaction tx = null;
- try {
- s = HibernateUtil.getSession();
- tx = s.beginTransaction();
- Teacher t = (Teacher) s.get(Teacher.class, id);
- System.out.println("students:" + t.getStudents().size());
- tx.commit();
- } finally {
- if (s != null)
- s.close();
- }
- }
- static void add() {
- Session s = null;
- Transaction tx = null;
- try {
- Set<Teacher> ts = new HashSet<Teacher>();
- Teacher t1 = new Teacher();
- t1.setName("t1 name");
- ts.add(t1);
- Teacher t2 = new Teacher();
- t2.setName("t2 name");
- ts.add(t2);
- Set<Student> ss = new HashSet<Student>();
- Student s1 = new Student();
- s1.setName("s1");
- ss.add(s1);
- Student s2 = new Student();
- s2.setName("s2");
- ss.add(s2);
- t1.setStudents(ss); //1
- t2.setStudents(ss); //1
- //
- // s1.setTeachers(ts); //2
- // s2.setTeachers(ts); //2
- s = HibernateUtil.getSession();
- tx = s.beginTransaction();
- s.save(t1);
- s.save(t2);
- s.save(s1);
- s.save(s2);
- tx.commit();
- } finally {
- if (s != null)
- s.close();
- }
- }
- }
public class Many2Many { public static void main(String[] args) { add(); //query(1); } static void query(int id) { Session s = null; Transaction tx = null; try { s = HibernateUtil.getSession(); tx = s.beginTransaction(); Teacher t = (Teacher) s.get(Teacher.class, id); System.out.println("students:" + t.getStudents().size()); tx.commit(); } finally { if (s != null) s.close(); } } static void add() { Session s = null; Transaction tx = null; try { Set<Teacher> ts = new HashSet<Teacher>(); Teacher t1 = new Teacher(); t1.setName("t1 name"); ts.add(t1); Teacher t2 = new Teacher(); t2.setName("t2 name"); ts.add(t2); Set<Student> ss = new HashSet<Student>(); Student s1 = new Student(); s1.setName("s1"); ss.add(s1); Student s2 = new Student(); s2.setName("s2"); ss.add(s2); t1.setStudents(ss); //1 t2.setStudents(ss); //1 // // s1.setTeachers(ts); //2 // s2.setTeachers(ts); //2 s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.save(t1); s.save(t2); s.save(s1); s.save(s2); tx.commit(); } finally { if (s != null) s.close(); } } }
运行此程序后:控制台打印的sql语句如下所示:
Hibernate: insert into Teacher (name) values (?)
Hibernate: insert into Teacher (name) values (?)
Hibernate: insert into Student (name) values (?)
Hibernate: insert into Student (name) values (?)
Hibernate: insert into teacher_student (teacher_id, student_id) values (?, ?)
Hibernate: insert into teacher_student (teacher_id, student_id) values (?, ?)
Hibernate: insert into teacher_student (teacher_id, student_id) values (?, ?)
Hibernate: insert into teacher_student (teacher_id, student_id) values (?, ?)
一共在中间表里面插入了4条记录。
中间表结构如下所示:
DROP TABLE IF EXISTS `test`.`teacher_student`;
CREATE TABLE `test`.`teacher_student` (
`teacher_id` int(11) NOT NULL,
`student_id` int(11) NOT NULL,
PRIMARY KEY (`student_id`,`teacher_id`),
KEY `FK2E2EF2DE6C8A2663` (`teacher_id`),
KEY `FK2E2EF2DE5BEEDBC3` (`student_id`),
CONSTRAINT `FK2E2EF2DE5BEEDBC3` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`),
CONSTRAINT `FK2E2EF2DE6C8A2663` FOREIGN KEY (`teacher_id`) REFERENCES `teacher` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
表中插入的记录如下所示:
mysql> select * from teacher_student;
+------------+------------+
| teacher_id | student_id |
+------------+------------+
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 2 |
+------------+------------+
4 rows in set (0.00 sec)
程序中注释为1的语句非常重要,它是建立Teacher与Student关联的语句,如果没有这两条语句,虽然程序照样会执行,但是在中间表teacher_student没有任何记录,也就是Teacher与Student之间未关联。
当然你也可以通过程序中注释为2的语句来建立Teacher与Student之间的关联关系,同样会产生与注释为1的语句的效果。但是你不能在程序中同时出现以上四句程序,否则会抛出异常( PRIMARY KEY (`student_id`,`teacher_id`),所以会出现主键冲突的异常 ),:
Hibernate: insert into teacher_student (teacher_id, student_id) values (?, ?)
Hibernate: insert into teacher_student (teacher_id, student_id) values (?, ?)
Hibernate: insert into teacher_student (teacher_id, student_id) values (?, ?)
Hibernate: insert into teacher_student (teacher_id, student_id) values (?, ?)
Hibernate: insert into teacher_student (student_id, teacher_id) values (?, ?)
Hibernate: insert into teacher_student (student_id, teacher_id) values (?, ?)
Hibernate: insert into teacher_student (student_id, teacher_id) values (?, ?)
Hibernate: insert into teacher_student (student_id, teacher_id) values (?, ?)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
解决上面产生异常的办法是设置inverse属性。即在Tearcher一端或Student一端设置inverse="true",即让他们之中的某一方放弃维护关联关系。此时,虽然上面四句程序在测试程序中同时出现了(其实就是在对象模型上相互设置了他们的关联关系),但程序照样能运行正常,因为了在数据库模型上,只会有两句程序生效,也就是没有设置inverse="true"的那一端会去维护关联关系。有关inverse的说细信息,可以参看我的文章hibernate级联(cascade和inverse).
执行测试程序中的查询测试,控制台打印的信息如下所示:
Hibernate: select teacher0_.id as id5_0_, teacher0_.name as name5_0_ from Teacher teacher0_ where teacher0_.id=?
Hibernate: select students0_.teacher_id as teacher1_1_, students0_.student_id as student2_1_, student1_.id as id7_0_, student1_.name as name7_0_ from teacher_student students0_ left outer join Student student1_ on students0_.student_id=student1_.id where students0_.teacher_id=?
students:2
从打印出的sql语句可以看出,多对多关系进行查询时,效率是比较低的。
相关推荐
在数据库设计中,Many-to-Many关系意味着两个实体之间存在多对多的关系。例如,学生可以选修多门课程,而一门课程也可以被多名学生选修。在Hibernate中,这种关系通过中间关联表(也称为连接表或联合表)来实现,该...
本话题主要探讨两种基本的关系映射类型:many-to-one(多对一)和one-to-many(一对多)。这两种关系映射是数据库中常见的关联类型,理解和掌握它们对于开发高质量的Java应用至关重要。 1. many-to-one关系 many-to...
"Hibernate one-to-many / many-to-one关系映射"是两个基本的关系类型,用于表示实体间的关联。在这个主题中,我们将深入探讨这两种关系映射的原理、配置和实际应用。 一、一对一(one-to-one)与一对多(one-to-...
### Hibernate Many-to-One (多对一) 及 Cascade (级联) #### 一、Many-to-One(多对一) 在数据库设计中,实体之间的关系主要包括一对一、一对多、多对多等几种类型。而在ORM(对象关系映射)框架Hibernate中,...
本示例将详细讲解如何在Hibernate中实现多对多(many-to-many)的关系映射。 在数据库设计中,多对多关系是指两个实体之间存在多个关联,比如学生和课程的关系,一个学生可以选修多门课程,一门课程也可以被多个...
在Java的持久化框架Hibernate中,Many-to-Many映射是一种常见的关系模型,它用于表示两个实体类之间多对多的关系。在这个主题中,我们将深入探讨如何使用注解来实现这种映射,以及其背后的数据库原理和实际应用。 ...
在Java世界中,Hibernate是一个非常...总结,`Hibernate one-to-many`注解是处理实体间多对一关系的关键工具,它简化了数据库操作,提升了开发效率。正确理解和使用这些注解,对于构建高效、可维护的Java应用至关重要。
在Java的持久化框架Hibernate中,Many-to-Many关系是一种常见的数据模型,它表示一个实体可以与多个其他实体之间存在关联,反之亦然。这篇博客文章可能会深入探讨如何在Hibernate中处理这种多对多的关系,并可能涉及...
本实例将详细讲解如何在Hibernate中实现Many-to-One关系映射,这是一种常见的数据库关联,表示一个实体可以与多个其他实体相关联。 在Many-to-One关系中,通常一个实体(如部门)可以有多个相关实体(如员工),而...
"Hibernate many-to-many"指的是Hibernate中处理多对多关联关系的一种方式。多对多关系是两个实体类之间存在的一种复杂关联,比如学生和课程之间的关系,一个学生可以选修多门课程,一门课程也可以被多个学生选修。 ...
在配置双向一对多关联时,我们通常在"多"一端(这里是`Student`)的集合属性上使用`<many-to-one>`标签,将`Classes`对象映射到数据库中的外键。同时,在"一"端(`Classes`)使用`<set>`标签,表示班级可以包含多个...
在Hibernate中,我们可以使用`<many-to-many>`标签来配置多对多映射。下面是一个简单的例子: ```xml <many-to-many column="USER_ID" foreign-key="FK_USER_ROLE" entity-name="com.example.Role" table="USER...
Hibernate中many-to-one关系的编写_远航的水手
在本文中,我们将深入探讨如何使用Hibernate来实现多对多(many-to-many)的映射关系。 首先,多对多映射关系通常涉及到两个实体类之间的关联,例如在学生选课的例子中,学生(Student)和课程(Course)之间存在多...
同时,`Account.hbm.xml`中的`many-to-one`元素将`column`属性设置为`FID`,表示通过`FID`字段关联到`User`实体。 #### 测试场景解释 在测试场景中,我们创建了一个`User`对象和一个`Account`对象,并在`User`对象...
Hibernate中many-to-one关系的编写_远航的水手.htm
#### 一、多对一关联(Many-to-One) 在本章中,我们将探讨如何在 Hibernate 中处理多对一关联。这种关联类型非常常见,在许多应用中都会遇到。例如,在一个在线书店应用中,每本书都与一个出版商关联,而一个出版...
《深入理解Hibernate:第三解——Many-to-One关系映射实战》 在Java世界里,ORM(Object-Relational Mapping)框架的出现极大地简化了数据库与Java对象之间的交互,而Hibernate作为其中的佼佼者,更是备受开发者青睐...
本程序包含: hibenate 上传图片,二进制数据,大文本,集合映射的多种关系,onetoone,ontomany,manytomany等关系详细代码示例以及注释,全部由本人测试通过,对理解hibernate配置有极大帮助。
标题"Hibernate one to many(many to one) 配置"涉及到的是关系型数据库在Java中的持久化框架Hibernate中的两种关联映射关系:一对一(One-to-One)和多对一(Many-to-One)。在数据库设计中,这种关系很常见,例如...