Search code examples
hibernatehibernate-enverspost-update

Envers 4.2.x adds a record to the AUD table even if there are no changes


I'm using Hibernate Envers 4.2.21.FINAL. As I want to track only some properties' changes, I added the annotation

@Audited(withModifiedFlag = true)

on them. The flag column "propname_MOD" is correctly created, but the problem is that when I save an object, all the MOD flags are set to true, and not only for the attribute I modified. Does anyone know how to solve this problem?

UPDATE I noticed that in the class org.hibernate.envers.event.EnversPostUpdateEventListenerImpl, when the following object is instantiated

AuditWorkUnit workUnit = new ModWorkUnit(
    event.getSession(),event.getPersister().getEntityName(),getAuditConfiguration(),
    event.getId(),event.getPersister(),newDbState,event.getOldState() );

event.getOldState() is null, but IMO it should be not null. This is why Envers is adding a new revision each time.


Solution

  • The problem was in fact related to the event.getOldState() being null.

    It was due to the saveOrUpdate method of Hibernate, which perceives the entity from JSF as detached. I now use merge instead of saveOrUpdate and Envers correctly stores the _MOD fields to true or false.