2 Replies Latest reply on Jan 9, 2013 7:29 AM by Shirish Bathe

    Conditional auditing problem

    Andrzej Cichon Newbie

      Hi, I've followed documentation chapter tutorial "15.8. Conditional auditing".

      here is what I've done so far:


      1. Turn off automatic Envers event listeners registration by setting the hibernate.listeners.envers.autoRegister Hibernate property to false.




      <property name="hibernateProperties">
                      <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                      <prop key="hibernate.show_sql">true</prop>
                       <prop key="hibernate.hbm2ddl.auto" >update</prop>
                      <prop key="hibernate.connection.useUnicode">true</prop>
                      <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
                      <prop key="hibernate.jdbc.batch_size">0</prop>
                      <prop key="org.hibernate.envers.audit_table_suffix">_version</prop>
                      <prop key="org.hibernate.envers.revision_field_name">version_id</prop>
                      <prop key="hibernate.listeners.envers.autoRegister">false</prop>

      2. Create subclasses for appropriate event listeners. For example, if you want to conditionally audit entity insertions, extend the org.hibernate.envers.eventEnversPostInsertEventListenerImpl class. Place the conditional-auditing logic in the subclasses, call the super method if auditing should be performed.




      public class CustomEnversListener extends EnversPostUpdateEventListenerImpl  {
          private static final long serialVersionUID = 1L;
          public CustomEnversListener(AuditConfiguration enversConfiguration) {
          public void onPostUpdate(PostUpdateEvent arg0) {
              System.out.println("!!! just logging entity !! "+arg0.getEntity());


      3. Create your own implementation of org.hibernate.integrator.spi.Integrator, similar to org.hibernate.envers.event.EnversIntegrator. Use your event listener classes instead of the default ones.


      I overrided extisting EnversIntegrator:


      public class CustomEnversIntegrator extends EnversIntegrator {
          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 );
              listenerRegistry.appendListeners(EventType.POST_UPDATE, new CustomEnversListener(enversConfiguration));


      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 to your jar. The file should contain the fully qualified name of the class implementing the interface.


      I have no idea what to do with it now. Any more explanation, please ?