一、多对一
多对一(或者一对多):在学生与老师的情况下,一个老师可以教多个学生,但一个学生只能被教一个老师教;
对于类:在多的那方拥有一的那方的一个实体
二、修改student.java和teacher.java类
复制03工程,并更名为Hibernate_04_ManyToOne;
在多的一方中添加@ManyToOne注解
1 package com.model; 2 3 import javax.persistence.Embedded; 4 import javax.persistence.Entity; 5 import javax.persistence.GeneratedValue; 6 import javax.persistence.Id; 7 import javax.persistence.ManyToOne; 8 import javax.persistence.OneToOne; 9 import javax.persistence.Table; 10 11 @Entity 12 @Table(name="student") 13 public class Student { 14 private int id; 15 private String name; 16 private int age; 17 private Teacher teacher; 18 19 @Id 20 @GeneratedValue 21 public int getId() { 22 return id; 23 } 24 public void setId(int id) { 25 this.id = id; 26 } 27 public String getName() { 28 return name; 29 } 30 public void setName(String name) { 31 this.name = name; 32 } 33 public int getAge() { 34 return age; 35 } 36 public void setAge(int age) { 37 this.age = age; 38 } 39 @ManyToOne 40 public Teacher getTeacher() { 41 return teacher; 42 } 43 public void setTeacher(Teacher teacher) { 44 this.teacher = teacher; 45 } 46 }
1 package com.model; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.Table; 7 8 @Entity 9 @Table(name="teacher") 10 public class Teacher { 11 private int id; 12 private String teacherName; 13 private int teacherAge; 14 private String title; 15 16 @Id 17 @GeneratedValue 18 public int getId() { 19 return id; 20 } 21 public void setId(int id) { 22 this.id = id; 23 } 24 public String getTeacherName() { 25 return teacherName; 26 } 27 public void setTeacherName(String teacherName) { 28 this.teacherName = teacherName; 29 } 30 public int getTeacherAge() { 31 return teacherAge; 32 } 33 public void setTeacherAge(int teacherAge) { 34 this.teacherAge = teacherAge; 35 } 36 public String getTitle() { 37 return title; 38 } 39 public void setTitle(String title) { 40 this.title = title; 41 } 42 }
修改xml,添加使用teacher.java类的注解的配置;
<mapping class="com.model.Teacher"/>
三、运行测试;
结果:
在student表中生成了一个外键,依赖teacher表,teacher表中没有特别的改变;
四、一对多
一对多:对于类,在一的那方存在多的那方的集合
五、修改student和teahcer类
复制04工程,并更名为Hibernate_05_OneToMany;
在一的那方添加集合类(推荐set),并添加@OneToMany注解
1 package com.model; 2 3 import javax.persistence.Embedded; 4 import javax.persistence.Entity; 5 import javax.persistence.GeneratedValue; 6 import javax.persistence.Id; 7 import javax.persistence.ManyToOne; 8 import javax.persistence.OneToOne; 9 import javax.persistence.Table; 10 11 @Entity 12 @Table(name="student") 13 public class Student { 14 private int id; 15 private String name; 16 private int age; 17 18 @Id 19 @GeneratedValue 20 public int getId() { 21 return id; 22 } 23 public void setId(int id) { 24 this.id = id; 25 } 26 public String getName() { 27 return name; 28 } 29 public void setName(String name) { 30 this.name = name; 31 } 32 public int getAge() { 33 return age; 34 } 35 public void setAge(int age) { 36 this.age = age; 37 } 38 }
1 package com.model; 2 3 import java.util.HashSet; 4 import java.util.Set; 5 6 import javax.persistence.Entity; 7 import javax.persistence.GeneratedValue; 8 import javax.persistence.Id; 9 import javax.persistence.OneToMany; 10 import javax.persistence.Table; 11 12 @Entity 13 @Table(name="teacher") 14 public class Teacher { 15 private int id; 16 private String teacherName; 17 private int teacherAge; 18 private String title; 19 private Set<Student> students = new HashSet<Student>(); 20 21 @Id 22 @GeneratedValue 23 public int getId() { 24 return id; 25 } 26 public void setId(int id) { 27 this.id = id; 28 } 29 public String getTeacherName() { 30 return teacherName; 31 } 32 public void setTeacherName(String teacherName) { 33 this.teacherName = teacherName; 34 } 35 public int getTeacherAge() { 36 return teacherAge; 37 } 38 public void setTeacherAge(int teacherAge) { 39 this.teacherAge = teacherAge; 40 } 41 public String getTitle() { 42 return title; 43 } 44 public void setTitle(String title) { 45 this.title = title; 46 } 47 @OneToMany 48 public Set<Student> getStudents() { 49 return students; 50 } 51 public void setStudents(Set<Student> students) { 52 this.students = students; 53 } 54 }
六、测试运行
结果生成了一张中间表teacher_student
PS:关于双向关联,可以在studenjava中添加字段Teacher teacher,并使用注解@ManyToOne;
PSS:有双向关联的情况,加上mappingBy;
----------------------------------------------------------------------------------------------------
此文为个人学习记录,如有参考转载,请注明出处 黑白熊的博客 http://www.cnblogs.com/xiong233/