Conditional Envers Auditing

hibernate envers
spring boot envers
hibernate envers custom audit table
import org hibernate envers audited
java auditing framework
spring audit logging example

I have a requirement where I want to audit records only on change of Status field. I've followed documentation chapter tutorial "15.8. Conditional auditing".

Step 1: Turn off automatic Envers event listeners registration. I have following:

<prop key="hibernate.listeners.envers.autoRegister">false</prop>

Step 2: Create subclasses for appropriate event listeners.

public class DeleteEnversListener extends EnversPostDeleteEventListenerImpl {   
    private static final long serialVersionUID = 5906427978349712224L;
    private static Log log = LogFactory.getLog(DeleteEnversListener.class);

    public DeleteEnversListener(AuditConfiguration enversConfiguration) {
        super(enversConfiguration);
    }

    @Override
    public void onPostDelete(PostDeleteEvent event) {
        log.info("!!! just logging entity !! "+ event.getEntity());
        super.onPostDelete(event);
    }   
}

In similar way, I have

  • InsertEnversListener
  • UpdateEnversListener
  • DeleteEnversListener
  • CollectionRecreateEnversListener
  • PreCollectionRemoveEnversListener
  • PreCollectionUpdateEnversListener

Step 3: Create your own implementation of org.hibernate.integrator.spi.Integrator

public class CustomEnversIntegrator extends EnversIntegrator   {

    private static Log log = LogFactory.getLog(CustomEnversIntegrator.class);

    @Override
    public void integrate(Configuration configuration,
            SessionFactoryImplementor sessionFactory,
            SessionFactoryServiceRegistry serviceRegistry) {

        super.integrate(configuration, sessionFactory, serviceRegistry);
        final AuditConfiguration enversConfiguration = AuditConfiguration.getFor( configuration, serviceRegistry.getService( ClassLoaderService.class ) );
        EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );

        System.out.println("Registering event listeners");
        if (enversConfiguration.getEntCfg().hasAuditedEntities()) {
            listenerRegistry.appendListeners(EventType.POST_INSERT, new InsertEnversListener(enversConfiguration));
            listenerRegistry.appendListeners(EventType.POST_UPDATE, new UpdateEnversListener(enversConfiguration));
            listenerRegistry.appendListeners(EventType.POST_DELETE, new DeleteEnversListener(enversConfiguration ) );
            listenerRegistry.appendListeners(EventType.POST_COLLECTION_RECREATE, new CollectionRecreateEnversListener(enversConfiguration ) );
            listenerRegistry.appendListeners(EventType.PRE_COLLECTION_REMOVE, new PreCollectionRemoveEnversListener(enversConfiguration ) );
            listenerRegistry.appendListeners(EventType.PRE_COLLECTION_UPDATE, new PreCollectionUpdateEnversListener(enversConfiguration ) );
        }

    }
}

Step 4: For the integrator to be automatically used when Hibernate starts up, you will need to add a META-INF/services/org.hibernate.integrator.spi.Integrator file. Here is content of org.hibernate.integrator.spi.Integrator file

com.hib.sample.listener.CustomEnversIntegrator

I am not sure, if I am missing anything. I am using JBOSS AS 7.0 with Hibernate 4.1.8

Here is a Spring-only solution for the conditional Envers auditing without an ugly META-INF folder etc. All what you need is a bean in your configuration class and a CustomEnversEventListener.

@Bean
public EventListenerRegistry listenerRegistry(EntityManagerFactory entityManagerFactory) {
    ServiceRegistryImplementor serviceRegistry = entityManagerFactory.unwrap(SessionFactoryImpl.class).getServiceRegistry();

    final EnversService enversService = serviceRegistry.getService(EnversService.class);
    EventListenerRegistry listenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);

    listenerRegistry.setListeners(EventType.POST_UPDATE, new CustomEnversEventListener(enversService));
    return listenerRegistry;
  }

and

  public class CustomEnversEventListener extends EnversPostUpdateEventListenerImpl {

    CustomEnversEventListener(EnversService enversService) {
      super(enversService);
    }

    @Override
    public void onPostUpdate(PostUpdateEvent event) {

      // custom conditional stuff

      super.onPostUpdate(event);
    }
  }

If you want to customise only one listener i.e. EnversPostUpdateEventListener, you don't need to disable hibernate.listeners.envers.autoRegister in order to let Envers register the other listener.

Then you can override Envers listeners by listenerRegistry.setListeners or append by listenerRegistry.appendListeners

Conditional Envers Auditing, Here is a Spring-only solution for the conditional Envers auditing without an ugly META-INF folder etc. All what you need is a bean in your  Conditional Envers Auditing Step 1: Turn off automatic Envers event listeners registration. I have following: <prop key="hibernate.listeners.envers. Step 2: Create subclasses for appropriate event listeners. public class DeleteEnversListener extends Step 3: Create your own implementation of

Try to place integrator file into:

sample.war\WEB-INF\classes\META-INF\services\...

How to Implement Conditional Auditing with Hibernate Envers, But implementing a conditional audit requires more work. By default, Hibernate Envers registers a set of event listeners which are triggered by Hibernate ORM. Implementing Conditional Envers Auditing (Hibernate 4.3.7.Final) Ask Question Asked 4 years, 10 months ago. Active 2 years, 9 months ago. Viewed 2k times

Maybe...

In my case, I use Maven, and I had to include in the pom.xml, the following line : <include>**/*.Integrator</include>, because the file was not packaged in the .ear.

My pom.xml:

<resources>
       <resource>
           <directory>src/main/resources</directory>
           <filtering>true</filtering>
           <includes>
                <include>**/*.xml</include>
               <include>**/*.Integrator</include>
           </includes>
       </resource>
       ...

Hibernate Envers : How to Implement a Conditional Audit Log , The current conditional auditing solution works but its somewhat the state of the entity during the various event life cycles used by Envers. Well, it is, as long as you audit all changes performed on an entity. But implementing a conditional audit requires more work. By default, Hibernate Envers registers a set of event listeners which

creating a file org.hibernate.integrator.spi.Integrator (containing the qualified name of my custom integrator) in a folder META-INF/services/ under src/main/resources of my maven project made my custom integrator code be called.

[HHH-11326] Conditional Auditing through class-level annotations , For creating conditional auditing we need to turn off the auto registration of event listeners, hibernate.envers.  Conditional auditing Envers persists audit data in reaction to various Hibernate events (e.g. post update, post insert, and so on), using a series of even listeners from the org.hibernate.envers.event package. By default, if the Envers jar is in the classpath, the event listeners are auto-registered with Hibernate.

@ComponentScan(basePackages = {"com.example.demo"}, lazyInit = true)

Adding lazyInit = true, triggered the custom integrator for me.

How to Implement Hibernate Envers in an Application, Configure Conditional Auditing. Task Summary. Hibernate Envers persists audit data in reaction to various Hibernate events, using a series of event listeners. here is what I've done so far: 1. Turn off automatic Envers event listeners registration by setting the hibernate.listeners.envers.autoRegister 2. Create subclasses for appropriate event listeners. For example, if you want to conditionally audit entity insertions, 3. Create your own

10.7.5.3. Configure Conditional Auditing JBoss Enterprise , How to configure conditional auditing using Hibernate Envers? Solution Verified - Updated February 24 2014 at 6:28 AM -. English. No translations currently  The Envers project aims to enable easy auditing of persistent classes. All that you have to do is annotate your persistent class or some of its properties, that you want to audit, with @Audited. For each audited entity, a table will be created, which will hold the history of changes made to the entity.

How to configure conditional auditing using Hibernate Envers , I need to configure conditional auditing using Hibernate Envers and Spring. The default configuration works, but I want only the delete  Conditional auditing questions 1) From the logging I noticed that occasionally the "entity" associated to the event is an instance of a HashMap. This 2) In my test case I created three Users - none of the Users are enabled, then I create a single ProductOrder associated

Hibernate Community • View topic, I am doing lots of R&D to do auditing for a big project. So far I am considering Hibernate Envers as my best bet as it reduces the manual coding and its versioning features is very Yes you can do conditional auditing.

Comments
  • And what happens currently? Is your custom integrator code called at all?
  • @adamw: I have added sysout statements to integrator code, but none of them are appearing. It seems Services are not recognized by JBOSS. I have created project using Eclipse 4.2 Juno as Dynamic Web project. Not sure whether Location of META-INF is correct. Any advice..
  • Did you check the resulting jar/war, if it has the file in the correct place?
  • @Adamw, Sorry for delayed reply, Yes META-INF is located in WAR file at G:\jboss-as-web-7.0.2.Final\standalone\deployments\sample.war\META-INF\services\
  • And the classes are directly in the war as well, not in a .jar?