gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:26:24 26 4
gpt4 key购买 nike

我的应用程序使用 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

所以我更改了代码,实现了新的集成器接口(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 ) );
}

}

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

java.lang.IllegalStateException: Service is not yet initialized at 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 实例?

谢谢,安德鲁

最佳答案

我反编译了 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 ) );
}
}

问题解决了!

关于java - Hibernate Envers 4.3 迁移到 5.0 - 条件 Envers 审计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36764486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com