2015-05-20 6 views
11

Ho una tabella di controllo con la revisione e voglio creare un'altra tabella con una relazione uno-a-molti con la revisione della tabella di audit. La revisione dell'audit indicherà i nuovi dati della tabella. Come lo posso fare?Hibernate join table con altra tabella di controllo

+0

controllare la mia risposta dalla http://stackoverflow.com/q/30184227/845849 –

+0

Non voglio modificare la tabella revinfo Voglio solo usare la revisione come relazione con un'altra tabella. –

+0

Ahh ok capito in quel caso puoi semplicemente mappare l'entità di revisione predefinita (org.hibernate.envers.DefaultRevisionEntity) nella tabella delle entità richiesta. –

risposta

2

che ho fatto qualcosa di simile in un progetto che ha GPA degli studenti (tabella di controllo con le revisioni) e poi un tavolo con il CurrentGpa che punta sempre ad una revisione di piombo dalla tabella GPA. Per fare questo ho usato la seguente struttura:

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

    // ... 
} 
Problemi correlati