hibernate学习五(关系映射多对一与一对多)

时间:2022-03-05 07:26:18

一、多对一

  多对一(或者一对多):在学生与老师的情况下,一个老师可以教多个学生,但一个学生只能被教一个老师教;

  对于类:在多的那方拥有一的那方的一个实体

二、修改student.java和teacher.java类

  复制03工程,并更名为Hibernate_04_ManyToOne;

  在多的一方中添加@ManyToOne注解

 package com.model;

 import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table; @Entity
@Table(name="student")
public class Student {
private int id;
private String name;
private int age;
private Teacher teacher; @Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@ManyToOne
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
 package com.model;

 import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="teacher")
public class Teacher {
private int id;
private String teacherName;
private int teacherAge;
private String title; @Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public int getTeacherAge() {
return teacherAge;
}
public void setTeacherAge(int teacherAge) {
this.teacherAge = teacherAge;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}

修改xml,添加使用teacher.java类的注解的配置;

        <mapping class="com.model.Teacher"/>

三、运行测试;

  hibernate学习五(关系映射多对一与一对多)

  hibernate学习五(关系映射多对一与一对多)

  结果:

  在student表中生成了一个外键,依赖teacher表,teacher表中没有特别的改变;

四、一对多

  一对多:对于类,在一的那方存在多的那方的集合

五、修改student和teahcer类

  复制04工程,并更名为Hibernate_05_OneToMany;

  在一的那方添加集合类(推荐set),并添加@OneToMany注解

 package com.model;

 import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table; @Entity
@Table(name="student")
public class Student {
private int id;
private String name;
private int age; @Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
 package com.model;

 import java.util.HashSet;
import java.util.Set; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table; @Entity
@Table(name="teacher")
public class Teacher {
private int id;
private String teacherName;
private int teacherAge;
private String title;
private Set<Student> students = new HashSet<Student>(); @Id
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTeacherName() {
return teacherName;
}
public void setTeacherName(String teacherName) {
this.teacherName = teacherName;
}
public int getTeacherAge() {
return teacherAge;
}
public void setTeacherAge(int teacherAge) {
this.teacherAge = teacherAge;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@OneToMany
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}

六、测试运行

  hibernate学习五(关系映射多对一与一对多)

  结果生成了一张中间表teacher_student

  hibernate学习五(关系映射多对一与一对多)

PS:关于双向关联,可以在studenjava中添加字段Teacher teacher,并使用注解@ManyToOne;

PSS:有双向关联的情况,加上mappingBy;

----------------------------------------------------------------------------------------------------

此文为个人学习记录,如有参考转载,请注明出处 黑白熊的博客 http://www.cnblogs.com/xiong233/