I’ve been trying to use envers to automatically audit the below entity whenever there is an update performed on it. This does work and a table “mytable_aud” gets created with the columns
id, rev, revtype, name, offval, onval.
@Entity
@Getter
@Setter
@Table(name = "mytable")
@Audited
@EntityListeners(MyAuditManager.class)
public class MyEntity {
@Id
@Column(name = "id", nullable = false)
@SequenceGenerator(allocationSize = 1, name = "mytable_seq", sequenceName = "mytable_seq")
@GeneratedValue(generator = "mytable_seq", strategy = GenerationType.SEQUENCE)
private Long id;
@Column
private String name;
@Column(name = "onval")
private String onVal;
@Column(name = "offval")
private String offVal;
@Column
private Date modified;
}
Now I wanted to store some additional information - 2 columns, that only exist in the audit table.
I followed the documentation and various guides on the internet but nothing seems to work:
@Getter
@Setter
@Entity
@RevisionEntity(MyAuditManager.class)
public class MyEntityRevision extends DefaultRevisionEntity {
private Date fromDate;
private Date toDate;
}
This is my RevisionListener:
@NoArgsConstructor
@AllArgsConstructor
public class MyAuditManager implements RevisionListener {
private static final ThreadLocal<MyEntity> currentEntity = new ThreadLocal<>();
private EntityManager entityManager ;
@Override
public void newRevision(Object revisionEntity) {
MyEntityRevision rev = MyEntityRevision.class.cast(revisionEntity);
rev.setToDate(new Date());
rev.setFromDate(currentEntity.get().getModified());
clearEntity();
}
@PrePersist
@PreUpdate
public void setEntity(Object entity) {
if (entity instanceof MyEntity) {
currentEntity.set((MyEntity) entity);
}
}
public void clearEntity() {
currentEntity.remove();
}
The Listener is working correcly and the @PrePersist method as well as the newRevision method are being called correctly.
However, when the entity gets updated and the audit gets inserted, there are no columns “fromDate” and “toDate” to be found in the audit table.
Is this not the correct way to extend the default revision?
I’m Using hibernate 5.6.12 and Java 17