I need your help: I'm using hibernate envers 4.1.12 and I would like to enable only the delete listener, but I'm not able to do it. I know that starting from hibernate 4 there is no longer need to declare the listener in the configuration file, but by default all the operations are triggered in the audit tables and I need to monitor only the delete ones.

Could you help me?

Thanks in advance.

有帮助吗?

解决方案

Take a look at the Conditional audition section in the manual.

What you essentially need to do is:

  • turn off automatic listener registration
  • write a custom Integrator, which uses only the delete listener (basing on the EnversIntegrator)
  • setup the integrator

其他提示

I tell you what I did: since I'm using maven the project structure is well known [link] (http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html). Therefore in the src/main/resources I created a folder META-INF/services/ and added a file labaled org.hibernate.integrator.spi.Integrator. The file contains the name of the class that implements the EnversIntegrator interface (in my case: eu.dada.prov.sitelock.common.listener.CustomHibernateListenerIntegrator)That's it.

The class is:

@Component
public class CustomHibernateListenerIntegrator extends EnversIntegrator {
@Override
public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory,
        SessionFactoryServiceRegistry serviceRegistry) {

    EventListenerRegistry listenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
    listenerRegistry.addDuplicationStrategy(EnversListenerDuplicationStrategy.INSTANCE);

    final AuditConfiguration enversConfiguration = AuditConfiguration.getFor(configuration);

    if (enversConfiguration.getEntCfg().hasAuditedEntities()) {
        listenerRegistry.appendListeners(EventType.POST_DELETE, new DeleteEnversListener(enversConfiguration));
        listenerRegistry.appendListeners(EventType.PRE_UPDATE, new UpdateEnversListener(enversConfiguration));
        listenerRegistry.appendListeners(EventType.LOAD, new ProductLoadEventListener());
    }
}
}

Hope it helps.

Spring Boot 1.5.6

I was having the same problem using a more recent Hibernate with an on cascade delete: envers wanted to insert a record that referenced the deleted ID, throwing a constraint violation.

Just adding this answer, in case someone finds it useful.

Turn off all envers listeners in application.properties (and your equivalent test configuration if you have one).

spring.jpa.properties.hibernate.envers.autoRegisterListeners=false

Add the ones you want (here I only want insert and update).

 @Component
 public class HibernateListenerConfigurer
 {
   @PersistenceUnit
   private EntityManagerFactory entityManagerFactory;

   @PostConstruct
   protected void init()
   {
      SessionFactoryImpl sessionFactory = entityManagerFactory.unwrap(SessionFactoryImpl.class);
      EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
      EnversService enversService = sessionFactory.getServiceRegistry().getService(EnversService.class);

      registry.getEventListenerGroup(EventType.POST_INSERT).appendListener(new EnversPostInsertEventListenerImpl(enversService));
      registry.getEventListenerGroup(EventType.POST_UPDATE).appendListener(new EnversPostUpdateEventListenerImpl(enversService));
   }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top