Hibernate 框架总结(一):简单开发
Hibernate 框架总结(二):session的获取方式及hibernate对象的生命周期
Hibernate 框架总结(三):主键的生成策略
Hibernate 框架总结(五):查询
Hibernate 框架总结(六):缓存机制
表与表之间的关系
存在三种关系
一对一:丈夫——妻子, 学生——学生证
一对多:学生——班级, 皇帝——大臣
多对多:学生——课程, 学生——老师
配置关系映射的时候,无论是什么关系,要保证你中有我,我中有你的思想
一对一
开发一个husband实体类和一个wife实体类,两个表中都有name和age属性,两个类中互相拥有,就是Husband类中要有一个wife属性,Wife类中要有一个husband属性
两张表拥有关系,就肯定要有一张表来维系关系,通常来说多的哪一方来维系,一对一的关系中哪一方来维护都行,这里让husband来维护关系,既然husband来维系关系,那么husband表中肯定要增加一列,存放wife的id,而wife只需交给husband的来维护就行
Husband实体类:
package sdibt.fly.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
/**
* 丈夫实体类
* @author lxj
*
*/
@Entity
@Table(name="tb_husband",schema="hibernatedb")
public class Husband {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;//姓名
private int age;//年龄
//维护的一方即使是一对一的关系,这里也要写出ManyToOne
@ManyToOne
@JoinColumn(name="wid",unique=true)//在表中增加一列,而且值是唯一的
private Wife wife;
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
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;
}
}
Wife实体类
package sdibt.fly.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
/**
* 妻子实体类
* @author lxj
*
*/
@Entity
@Table(name="tb_wife",schema="hibernatedb")
public class Wife {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String name;//姓名
private int age;//年龄
//交给Husband中的Wife属性来维护,名字不能写错
@OneToOne(mappedBy="wife")
private Husband husband;
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
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;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/cb4b1f51bde9a200259f675e39ec8f7d.png)
一对多
班级与学生之间就是一对多的关系,一个班级可以有多个学生,但是一个学生只能对应一个班级,所有让学生表来维系关系,因为班级里可以有很多学生,所有Classes里的学生属性要是一个集合
Student类:
package sdibt.fly.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="tb_student",schema="hibernatedb")
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String stuName;
private int age;
//多对一,多的一方维系关系
@ManyToOne
@JoinColumn(name="class_id")
private Classes classes;
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Classes类:
package sdibt.fly.entity;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name="tb_classes",schema="hibernatedb")
public class Classes {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String clsName;
//一对多关系,存放sutdent集合
@OneToMany(mappedBy="classes")
private List<Student> students=new ArrayList<Student>();
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getClsName() {
return clsName;
}
public void setClsName(String clsName) {
this.clsName = clsName;
}
}
多对多
学生和老师之间是多对多关系,一个学生可以有多个老师,一个老师也可以有多个学生,当产生多对多关系时,就要创建一张表来维护两者之间的关,对student进行改动,增加一个teacher属性,因为Student表经常变化,所有维系关系也交给Student
Student类:
package sdibt.fly.entity;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="tb_student",schema="hibernatedb")
public class Student {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String stuName;
private int age;
@ManyToOne
@JoinColumn(name="class_id")
private Classes classes;//班级
@ManyToMany
//增加一张表,维护老师,学生之间的关系
@JoinTable(
name="st",//中间表的名字
schema="hibernatedb",//中间表属于哪个数据库
//关联当前表的外键名
joinColumns=@JoinColumn(name="sid"),
//关联对方表的外键名
inverseJoinColumns=@JoinColumn(name="tid")
)
private List<Teacher> teachers=new ArrayList<Teacher>();//老师
public List<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(List<Teacher> teachers) {
this.teachers = teachers;
}
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Teacher类:
package sdibt.fly.entity;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
@Entity
@Table(name="tb_teacher",schema="hibernatedb")
public class Teacher {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String tName;
//交给学生表来维系关系
@ManyToMany(mappedBy="teachers")
private List<Student> students=new ArrayList<Student>();
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String gettName() {
return tName;
}
public void settName(String tName) {
this.tName = tName;
}
}
生成的中间表: