1 Reply Latest reply on Jan 30, 2013 4:06 PM by Adam Warski

    Conditional Auditing

    Shirish Bathe Newbie

      I have a requirement where I want to audit records only on change of Status field.

       

      I have 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 simillar 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. Content of org.hibernate.integrator.spi.Integrator file are:

      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

       

      Please note that I have created dynamic Web project using Eclipse Juno and using default location of Meta-inf folder.

       

      Thanks,

      Shirish