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
Try to place integrator file into:
sample.war\WEB-INF\classes\META-INF\services\...
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>
...
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.
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
@ComponentScan(basePackages = {"com.example.demo"}, lazyInit = true)
Adding lazyInit = true, triggered the custom integrator for me.