前言
JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体[对象持久化]到数据库中。
Sun引入新的JPA ORM规范出于两个原因:其一,简化现有Java EE和Java SE应用开发工作;其二,Sun希望整合ORM技术,实现天下归一。(出自百度百科)
JPA优点:上手快,使用简单。
JPA缺点:不适合关联关系复杂的项目。
正题
正常情况下使用@ManyToMany注解描述多对多关系时,JPA自动生成的表中是只包括两个对应实体的主键信息的。有些时候呢,就会遇到需要在关联表中加入想要的其他字段来辅助完成需求。比如部门与部门人之间关系。一个部门有多个人,一个又可以属于多个部门,一个人在不同的部门之间有不同的身份,在做展示的时候就涉及排序问题,一个人在不同的部门拥有不一样的排序。
这里以老师,学生,老师对每个学生的印象分来进行编码。主体思路就是将@ManyToMany生成的关系表单独作为一个类,使用两个@ManyToOne来进行实现。
一、老师实体类
package com.ysh.springboot.permissions.test;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
/**
*
* @author ysh
* @date 2020/01/03
*/
@Entity
@Table(name = "teacher")
@Getter
@Setter
public class Teacher {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
@OneToMany(mappedBy = "teacher", fetch = FetchType.EAGER,cascade = CascadeType.ALL)
private List<TeacherStudent> teacherStudents;
}
二、学生实体类
package com.ysh.springboot.permissions.test;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
/**
*
* @author ysh
* @date 2020/01/03
*/
@Entity
@Table(name = "student")
@Getter
@Setter
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy="student", fetch = FetchType.EAGER,cascade=CascadeType.ALL)
private List<TeacherStudent> teacherStudents;
}
三、老师对学生印象分类
package com.ysh.springboot.permissions.test;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import lombok.Getter;
import lombok.Setter;
/**
*
* @author ysh
* @date 2020/01/03
*/
@Entity
@Table(name = "teacher_student")
@Getter
@Setter
public class TeacherStudent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "t_id")
private Teacher teacher;
@ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "s_id")
private Student student;
@JoinColumn()
@Column(nullable=true,name="impression_score")
private int impressionScore;
}
四、表生成结构
五、使用
通过关系表类来获取需要的辅助字段。