我的应用程序使用envers将数据写入_aud表,并将其包装到写入另一个表的xml中。 我在Envers 4.3中使用条件审计来完成。我的课程扩展了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:服务尚未初始化 在org.hibernate.envers.boot.internal.EnversServiceImpl.getEntitiesConfigurations(EnversServiceImpl.java:253)
我尝试检索EnversService,就像我使用旧的AuditConfiguration一样,没有任何结果。 我阅读了官方指南(http://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/Hibernate_User_Guide.html),但我发现没有什么可以帮助我。
如何检索可用于自定义侦听器的EnversService实例?
谢谢,安德鲁
答案 0 :(得分:1)
我反编译org.hibernate.envers.boot.internal.EnversIntegrator类并找到解决方案。
@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 ) );
}
}
解决了问题!