和基于配置文件的多对一双向关联大体一致,测试类无需变动,撤销XML的配置文件,将hibernate.cfg.xml中hbm的引入换成class的引入,如下
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
5 <hibernate-configuration>
6 <session-factory name="sessionFactory">
7 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
8 <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_begin</property>
9 <property name="hibernate.connection.username">root</property>
10 <property name="hibernate.connection.password">root</property>
11 <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
12 <property name="hibernate.hbm2ddl.auto">update</property>
13 <property name="show_sql">true</property>
14 <!-- <mapping resource="domain/Course.hbm.xml"/>
15 <mapping resource="domain/Student.hbm.xml"/>
16 <mapping resource="domain/StudentCourse.hbm.xml"/> -->
17 <mapping class="domain.Student"/>
18 <mapping class="domain.Teacher"/>
19 </session-factory>
20 </hibernate-configuration>
21
同时为相关的实体类添加注解
Student
1 package domain;
2
3 import javax.persistence.Entity;
4 import javax.persistence.GeneratedValue;
5 import javax.persistence.Id;
6 import javax.persistence.JoinColumn;
7 import javax.persistence.ManyToOne;
8 import javax.persistence.Table;
9
10 @Entity
11 @Table(name="t_student")
12 public class Student {
13
14 private int id;
15 private String name;
16 private String grade;
17 private Teacher teacher;
18
19 public Student() {
20 super();
21 }
22
23 public Student(String name, String grade, Teacher teacher) {
24 super();
25 this.name = name;
26 this.grade = grade;
27 this.teacher = teacher;
28 }
29
30 //设置自增主键
31 @Id
32 @GeneratedValue
33 public int getId() {
34 return id;
35 }
36 public void setId(int id) {
37 this.id = id;
38 }
39 public String getName() {
40 return name;
41 }
42 public void setName(String name) {
43 this.name = name;
44 }
45 public String getGrade() {
46 return grade;
47 }
48 public void setGrade(String grade) {
49 this.grade = grade;
50 }
51
52 //为实体类添加映射和外键
53 @ManyToOne
54 @JoinColumn(name="tid")
55 public Teacher getTeacher() {
56 return teacher;
57 }
58 public void setTeacher(Teacher teacher) {
59 this.teacher = teacher;
60 }
61
62 }
63
Teacher
1 package domain;
2
3 import java.util.Set;
4
5 import javax.persistence.Entity;
6 import javax.persistence.GeneratedValue;
7 import javax.persistence.Id;
8 import javax.persistence.OneToMany;
9 import javax.persistence.Table;
10
11 import org.hibernate.annotations.LazyCollection;
12 import org.hibernate.annotations.LazyCollectionOption;
13
14 @Entity
15 @Table(name="t_teacher")
16 public class Teacher {
17
18 private int id;
19 private String name;
20 private String course;
21 private Set<Student> studs;
22
23 public Teacher() {
24 super();
25 }
26
27 public Teacher(String name, String course) {
28 super();
29 this.name = name;
30 this.course = course;
31 }
32
33 @Id
34 @GeneratedValue
35 public int getId() {
36 return id;
37 }
38 public void setId(int id) {
39 this.id = id;
40 }
41 public String getName() {
42 return name;
43 }
44 public void setName(String name) {
45 this.name = name;
46 }
47 public String getCourse() {
48 return course;
49 }
50 public void setCourse(String course) {
51 this.course = course;
52 }
53
54 //mappedBy就意为将关系维护给到对方的teacher属性,相当于inverse=true
55 @OneToMany(mappedBy = "teacher")
56 //LazyCollectionOption.EXTRA相当于lazy="extra"
57 @LazyCollection(LazyCollectionOption.EXTRA)
58 public Set<Student> getStuds() {
59 return studs;
60 }
61
62 public void setStuds(Set<Student> studs) {
63 this.studs = studs;
64 }
65
66 }
67