I have an audit table with revisioning and I want to create another table with one-to-many relation with the audit table revision. The audit revision will pointing to the new table data. How can I do that?
Asked
Active
Viewed 3,025 times
10
-
Check my answer out from http://stackoverflow.com/q/30184227/845849 – Suken Shah May 20 '15 at 14:58
-
I do not want to change the revinfo table I just want to use the revision as relation to other table. – Jordan Borisov May 20 '15 at 15:01
-
Ahh ok understood in that case you can simply map the default revision entity (org.hibernate.envers.DefaultRevisionEntity) in your required entity table. – Suken Shah May 20 '15 at 16:17
-
1OK thanks can you put your comment as answer with some example please to mark it as it? :) – Jordan Borisov May 20 '15 at 18:38
-
If my answer below helped, would you mind accepting it? – Mark Roper Apr 28 '16 at 16:51
1 Answers
2
I've done something similiar in a project that has student GPAs (audit table with revisions) and then a table with the CurrentGpa
which always points to a lead revision from the GPA
table. To do this I used the following structure:
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;
}
// ...
}

Naros
- 19,928
- 3
- 41
- 71

Mark Roper
- 1,389
- 1
- 16
- 27