Search code examples
javahibernatehibernate-envers

Get all changes made to children entities with Hibernate Envers


@Entity
class Foo {
    List<Bar> bars;
}

How do I get all changes which were made on my object having many children using Hibernate Envers? I am interested in knowing all changes made to bars (i.e. creation, modification, deletion)

https://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/envers/AuditReader.html


Solution

  • Please create a custom event listener which extends org.hibernate.envers.event.AuditEventListener. You can then override anyone of the following methods to record the changes:

    1. onPostRecreateCollection
    2. onPreRemoveCollection
    3. onPreUpdateCollection

    You can register your custom listener in EntityManager configuration. Following is a simple example:

    <prop key="hibernate.ejb.event.pre-collection-update">
      com.bla.bla.audit.listener.AuditEventListener
    </prop>
    <prop key="hibernate.ejb.event.pre-collection-remove">
      com.bla.bla.audit.listener.AuditEventListener
    </prop>
    <prop key="hibernate.ejb.event.post-collection-recreate">
      com.bla.bla.audit.listener.AuditEventListener
    </prop>