Hibernate Envers 4.3 迁移到 5.0 - 条件 envers 审计

Hibernate Envers 4.3 migration to 5.0 - conditional envers auditing

我的应用程序使用 envers 将数据写入 _aud tables 并将其包装到一个 xml 中,然后写入另一个 table。 我在 Envers 4.3 中使用条件审计来完成它。我的 class 扩展了 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 ) );
    }   
}

在 Envers 5.0 中 AuditConfiguration 被移除 (https://github.com/hibernate/hibernate-orm/blob/5.0/migration-guide.adoc) 优先选择新 org.hibernate.envers.boot.internal.EnversService

所以我更改了我的代码,实现了新的 Integrator 接口

@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 ) );
    }

}

此代码不起作用,因为 EnversService 未初始化,给我一个

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

我尝试像处理旧的 AuditConfiguration 一样检索 EnversService,但没有任何结果。 我阅读了官方指南 (http://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/Hibernate_User_Guide.html),但没有找到任何可以帮助我的东西。

如何检索可用于我的自定义侦听器的 EnversService 实例?

谢谢,安德鲁

我反编译org.hibernate.envers.boot.internal.EnversIntegratorclass找到解决方法

@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 ) );
    }
}

问题已解决!