Hibernate连接表与其他审计表

时间:2015-05-20 14:50:28

标签: hibernate one-to-many hibernate-envers

我有一个带有修订的审计表,我想创建另一个与审计表修订版具有一对多关系的表。审计修订将指向新表数据。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

我在具有学生GPA(带有修订的审核表)的项目中做了类似的事情,然后是一个CurrentGpa的表,它始终指向GPA表的主要修订版。为此,我使用了以下结构:

<强> 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;
  }

  // ...
}