和多对一单向关联类似,但是双向关联需要在关联的两方都添加配置。实体类和配置文件如下,还是以老师和学生为例。
Student
1 package domain;
2
3
4 public class Student {
5
6 private int id;
7 private String name;
8 private String grade;
9
10 private Teacher teacher;
11
12 //get/set和构造省略,但实际不可省
13
14 }
15
1 <?xml version="1.0"?>
2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4
5 <hibernate-mapping>
6 <class name="domain.Student" table="STUDENT">
7 <id name="id" type="int">
8 <column name="ID" />
9 <generator class="native" />
10 </id>
11 <property name="name" type="java.lang.String">
12 <column name="NAME" />
13 </property>
14 <property name="grade" type="java.lang.String">
15 <column name="GRADE" />
16 </property>
17 <many-to-one name="teacher" class="domain.Teacher" fetch="join">
18 <!-- 指定对方的外键 -->
19 <column name="tid" />
20 </many-to-one>
21 </class>
22 </hibernate-mapping>
23
Teacher
1 package domain;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 public class Teacher {
7
8 private int id;
9 private String name;
10 private String course;
11
12 private Set<Student> students;
13
14 //get/set和构造省略,但实际不可省
15
16 }
17
1 <?xml version="1.0"?>
2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4
5 <hibernate-mapping>
6 <class name="domain.Teacher" table="TEACHER">
7 <id name="id" type="int">
8 <column name="ID" />
9 <generator class="native" />
10 </id>
11 <property name="name" type="java.lang.String">
12 <column name="NAME" />
13 </property>
14 <property name="course" type="java.lang.String">
15 <column name="COURSE" />
16 </property>
17 <!-- lazy extra会增强查询的功能,根据查询的内容选择查询语句,比如查询集合的数量会使用select count(*) -->
18 <!-- inverse true表示由对方来维护关联关系,也就是说,在学生对象设置老师属性并保存之后,二者的关系会建立,教师不负责维护关系 -->
19 <set name="students" lazy="extra" inverse="true">
20 <key>
21 <!-- 指定需要关联的外键,应和Student的设置一致 -->
22 <column name="tid" />
23 </key>
24 <one-to-many class="domain.Student"/>
25 </set>
26 </class>
27 </hibernate-mapping>
28
书写测试类
1 package demo;
2
3 import org.hibernate.Session;
4 import org.junit.Test;
5
6 import domain.Student;
7 import domain.Teacher;
8 import util.HibernateUtil;
9
10
11 public class App
12 {
13 @Test
14 /**
15 * oneToMany双向关联,设置多的一方维护关系。最佳实践就是:先添加一的一方,之后再添加多的一方
16 * 这样不会造成冗余的sql
17 */
18 public void addTest() {
19 Session session = null;
20 try {
21 session = HibernateUtil.openSession();
22 session.beginTransaction();
23
24 //添加一的一方
25 Teacher teacher = new Teacher();
26 teacher.setName("张老师");
27 teacher.setCourse("物理");
28 session.save(teacher);
29
30 //添加多的一方,设置一的一方,因为多的一方需要维护关系
31 Student stu1 = new Student();
32 stu1.setName("小明");
33 stu1.setGrade("一");
34 stu1.setTeacher(teacher);
35 session.save(stu1);
36
37 Student stu2 = new Student();
38 stu2.setName("小红");
39 stu2.setGrade("二");
40 stu2.setTeacher(teacher);
41 session.save(stu2);
42
43 session.getTransaction().commit();
44
45 } catch (Exception e) {
46 if (session != null) {
47 session.getTransaction().rollback();
48 }
49 } finally{
50 if (session != null) {
51 session.close();
52 }
53 }
54 }
55 }
56