javaspringhibernatejpahibernate-envers

Hibernate Envers conditional auditing, custom EnversIntegrator ignored at runtime


I need to configure conditional auditing using Hibernate Envers and Spring.

The default configuration works, but I want only the delete operations to be audited, saving the deleted entity in the audit table.

I've followed all the documentation steps but when I try the application with a simple main that creates and deletes an entity, my CustomEnversIntegrator is not called, instead the org.hibernate.envers.event.spi.EnversIntegrator is called.

Here is my spring-config.xml:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaProperties">
        <props>
            <prop key="org.hibernate.envers.default_schema">macap_auditoria</prop>
            <prop key="org.hibernate.envers.audit_table_prefix">aud_</prop>
            <prop key="org.hibernate.envers.audit_table_suffix"></prop>
            <prop key="org.hibernate.envers.store_data_at_delete">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
            <prop key="hibernate.listeners.envers.autoRegister">false</prop>
            <prop key="org.hibernate.envers.cascade_delete_revision">true</prop>
        </props>
    </property>
    ......

CustomEnversIntegrator.java, listening only to the delete operation:

    public class CustomEnversIntegrator extends EnversIntegrator {

private AuditConfiguration enversConfiguration;

@Override
public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    final EventListenerRegistry listenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
    listenerRegistry.addDuplicationStrategy(EnversListenerDuplicationStrategy.INSTANCE);
    enversConfiguration = AuditConfiguration.getFor(configuration, serviceRegistry.getService(ClassLoaderService.class));
    if (enversConfiguration.getEntCfg().hasAuditedEntities()) {
        listenerRegistry.prependListeners(EventType.POST_DELETE, new CustomEnversPostDeleteEventListener(enversConfiguration));
    }
}

}

And lastly, I've added the file META-INF/services/org.hibernate.spi.Integrator with one line inside: uy.com.macap.ccd.services.persistence.audit.CustomEnversIntegrator

The jar generated in my .m2 folder contains the META-INF/services/org.hibernate.spi.Integrator file inside, so I don't know what else to try besides recompiling envers with the EnversIntegrator class modified by me.

Thanks.


Solution

  • I think the problem is the name the of the file under META-INF/services. According to the documentation, it should be

    org.hibernate.integrator.spi.Integrator
    

    and not

    org.hibernate.spi.Integrator
    

    as in your post.