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

    Conditional auditing problem

    acichon89

      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.

       

      Done:

       

      <property name="hibernateProperties">
                  <props>
                      <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>
                  </props>
       </property>
      

      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.

       

      Done:

       

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

       

      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 {
      
          @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 );
              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 ?

        • 1. Re: Conditional auditing problem
          adamw

          Well did you complete step 4? That is, does your jar contain a META-INF/services/org.hibernate.integrator.spi.Integrator file with the CustomEnversIntegrator fully qualified class name?

           

          Adam

          • 2. Re: Conditional auditing problem
            bathe

            Hi Adam,

             

            I have followed all steps specified in 15.8. Conditional auditing including step 4.

             

            Here is content of my META-INF/services/org.hibernate.integrator.spi.Integrator file.

            com.hib.sample.listener.CustomEnversIntegrator

             

             

            Content of CustomEnversIntegrator and CustomEnversListener are same as listed in this discussion.

             

            Jar file contains Integrator file and related classes. But Auditing not happening.  If I make autoRegister -> true, Auditing is working fine. But I want to implement Conditional Auditing.

             

            Please help.

             

            - Shirish