Search code examples
javahibernatehibernate-enversauditing

Hibernate Envers 4.3 migration to 5.0 - conditional envers auditing


My application uses envers to write data to _aud tables and also to wrap it into an xml that is written into another table. I do it in Envers 4.3 with conditional auditing. My class 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, serviceRegistry.getService( ClassLoaderService.class ) );  
    if (enversConfiguration.getEntCfg().hasAuditedEntities()) 
    {          
        listenerRegistry.appendListeners( EventType.POST_UPDATE, new PostUpdateListenerLog( enversConfiguration ) );
        listenerRegistry.appendListeners( EventType.POST_INSERT, new PostInsertListenerLog( enversConfiguration ) );
        listenerRegistry.appendListeners( EventType.POST_DELETE, new PostDeleteListenerLog( enversConfiguration ) );
    }   
}

In Envers 5.0 AuditConfiguration was removed (https://github.com/hibernate/hibernate-orm/blob/5.0/migration-guide.adoc) in preference for new org.hibernate.envers.boot.internal.EnversService

so I change my code, implementing the new Integrator interface

@Override
public void integrate(Metadata mtdt, SessionFactoryImplementor sfi, SessionFactoryServiceRegistry serviceRegistry) {

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

    EnversService enversService = new EnversServiceImpl();
    if(enversService.getEntitiesConfigurations().hasAuditedEntities()) {
        listenerRegistry.appendListeners( EventType.POST_UPDATE, new PostUpdateListenerLog( enversService ) );
        listenerRegistry.appendListeners( EventType.POST_INSERT, new PostInsertListenerLog( enversService ) );
        listenerRegistry.appendListeners( EventType.POST_DELETE, new PostDeleteListenerLog( enversService ) );
    }

}

this code doesn't work because the EnversService is not initialized, giving me a

java.lang.IllegalStateException: Service is not yet initialized at org.hibernate.envers.boot.internal.EnversServiceImpl.getEntitiesConfigurations(EnversServiceImpl.java:253)

I tried to retrieve the EnversService as I did with the old AuditConfiguration without any result. I read the official guide (http://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/Hibernate_User_Guide.html) and I found nothing that could help me.

What can I do to retrieve an EnversService instance usable for my custom listeners?

Thanks, Andrew


Solution

  • I decompile org.hibernate.envers.boot.internal.EnversIntegrator class and find the solution.

    @Override
    public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
    
        EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );
        listenerRegistry.addDuplicationStrategy( EnversListenerDuplicationStrategy.INSTANCE );  
    
        EnversService enversService = serviceRegistry.getService(EnversService.class);
        if (!enversService.isInitialized()) {
            throw new HibernateException("Expecting EnversService to have been initialized prior to call to EnversIntegrator#integrate");
        }
        if(enversService.getEntitiesConfigurations().hasAuditedEntities()) {
            listenerRegistry.appendListeners( EventType.POST_UPDATE, new PostUpdateListenerLog( enversService ) );
            listenerRegistry.appendListeners( EventType.POST_INSERT, new PostInsertListenerLog( enversService ) );
            listenerRegistry.appendListeners( EventType.POST_DELETE, new PostDeleteListenerLog( enversService ) );
        }
    }
    

    Problem Solved!