Conditional control

I have a requirement when I want to check records only when the status field changes. I have completed the tutorial on documentation "15.8. Conditional Audit".

Step 1: Disable automatic registration of event listeners. I have the following:

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

Step 2: Subclass the corresponding 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); } } 

Similarly, 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: To automatically use the integrator when starting Hibernate, you need to add the file META-INF/services/org.hibernate.integrator.spi.Integrator . Here is the contents of the org.hibernate.integrator.spi.Integrator file

 com.hib.sample.listener.CustomEnversIntegrator 

I am not sure if I will not lose anything. I am using JBOSS AS 7.0 with Hibernate 4.1.8

+8
hibernate hibernate-envers
source share
5 answers

Try putting the integrator file in:

 sample.war\WEB-INF\classes\META-INF\services\... 
+1
source share

Here is the Spring-only Envers conditional audit Envers without the ugly META-INF folder, etc. All you need is a bean in your configuration class and 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 configure only one listener, i.e. EnversPostUpdateEventListener, you do not need to disable hibernate.listeners.envers.autoRegister so that Envers can register another listener.

You can then override the Envers listenerRegistry.setListeners using listenerRegistry.setListeners or add listenerRegistry.appendListeners

+1
source share

May be...

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

My pom.xml :

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

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

0
source share

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

Adding lazyInit = true caused my own integrator for me.

0
source share

All Articles