0

We are using Hibernate Envers to do our logging, but there was a requirement to set the type of event on the object which we are tracking the history. The property is required so we had the idea to put it to null each time the lastEvent wasn't set.

So we tried to set the property with @PrePersist

@PrePersist
public void checkIfLastEventWasModified() {
    if (!lastEventModified) {
        lastEvent = null;
    }
}

But apparantly hibernate considers these interceptors to mark the object as dirty or not. So when this code was run on the dirty check, lastEventModified is still false and lastEvent will be set to null each time even if nothing changed on the object.

We also tried it with the EventListeners from Hibernate but they ran outside the transaction and we could set the property, but they weren't saved to the db.

Both solutions don't feel right, but I have no clue how we can solve this problem in the best way.

jgeerts
  • 641
  • 8
  • 17

1 Answers1

0

If your are using a Hibernate version above 3.6.8, you might want to create your own EntityTrackingRevisionListener (javadoc). It is called every time an entity is changed (i.e. every time a new revision is created), and you can change the properties of an entity that is audited by Envers before it is persisted.

Jim Holden
  • 1,196
  • 1
  • 16
  • 37