Hibernate 加入 table 与其他审计 table
Hibernate join table with other audit table
我有一个审核 table 修订版,我想创建另一个 table 与审核 table 修订版具有一对多关系。审核修订将指向新的 table 数据。我该怎么做?
我在一个项目中做过类似的事情,该项目有学生 GPA(审核 table 和修改),然后 table 和 CurrentGpa
总是指向领先修订来自 GPA
table。为此,我使用了以下结构:
CurrentGpa.java
@Entity(name = HibernateConsts.CURRENT_GPA_TABLE)
public class CurrentGpa {
protected Gpa gpa;
protected Student student;
protected Long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = HibernateConsts.CURRENT_GPA_ID)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToOne(optional = true, fetch= FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = HibernateConsts.GPA_FK)
public Gpa getGpa() {
return gpa;
}
public void setGpa(Gpa gpa) {
this.gpa = gpa;
}
@OneToOne(optional = true, fetch= FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = HibernateConsts.STUDENT_FK)
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
// ...
}
Gpa.java
@Entity(name = HibernateConsts.GPA_TABLE)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Gpa {
protected Long studentId;
protected Double score;
protected LocalDate startDate;
protected LocalDate endDate;
protected LocalDate calculationDate;
protected Long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = HibernateConsts.GPA_ID)
public Long getId() { return id; }
public void setId(Long id) {
this.id = id;
}
@Column(name = HibernateConsts.STUDENT_FK)
public Long getStudentId() {
return studentId;
}
public void setStudentId(Long studentId) {
this.studentId = studentId;
}
@Column(name = HibernateConsts.GPA_SCORE)
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
@Column(name = HibernateConsts.GPA_START_DATE)
public LocalDate getStartDate() {
return startDate;
}
public void setStartDate(LocalDate startDate) {
this.startDate = startDate;
}
@Column(name = HibernateConsts.GPA_END_DATE)
public LocalDate getEndDate() {
return endDate;
}
// ...
}
我有一个审核 table 修订版,我想创建另一个 table 与审核 table 修订版具有一对多关系。审核修订将指向新的 table 数据。我该怎么做?
我在一个项目中做过类似的事情,该项目有学生 GPA(审核 table 和修改),然后 table 和 CurrentGpa
总是指向领先修订来自 GPA
table。为此,我使用了以下结构:
CurrentGpa.java
@Entity(name = HibernateConsts.CURRENT_GPA_TABLE)
public class CurrentGpa {
protected Gpa gpa;
protected Student student;
protected Long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = HibernateConsts.CURRENT_GPA_ID)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@OneToOne(optional = true, fetch= FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = HibernateConsts.GPA_FK)
public Gpa getGpa() {
return gpa;
}
public void setGpa(Gpa gpa) {
this.gpa = gpa;
}
@OneToOne(optional = true, fetch= FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name = HibernateConsts.STUDENT_FK)
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
// ...
}
Gpa.java
@Entity(name = HibernateConsts.GPA_TABLE)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class Gpa {
protected Long studentId;
protected Double score;
protected LocalDate startDate;
protected LocalDate endDate;
protected LocalDate calculationDate;
protected Long id;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = HibernateConsts.GPA_ID)
public Long getId() { return id; }
public void setId(Long id) {
this.id = id;
}
@Column(name = HibernateConsts.STUDENT_FK)
public Long getStudentId() {
return studentId;
}
public void setStudentId(Long studentId) {
this.studentId = studentId;
}
@Column(name = HibernateConsts.GPA_SCORE)
public Double getScore() {
return score;
}
public void setScore(Double score) {
this.score = score;
}
@Column(name = HibernateConsts.GPA_START_DATE)
public LocalDate getStartDate() {
return startDate;
}
public void setStartDate(LocalDate startDate) {
this.startDate = startDate;
}
@Column(name = HibernateConsts.GPA_END_DATE)
public LocalDate getEndDate() {
return endDate;
}
// ...
}