我有一个要求,我只想在更改状态字段时审核记录。我遵循了文档章节教程“15.8。条件审计”。
第1步:关闭自动Envers事件侦听器注册。我有以下内容:
<prop key="hibernate.listeners.envers.autoRegister">false</prop>
第2步:为适当的事件侦听器创建子类。
public class DeleteEnversListener extends EnversPostDeleteEventListenerImpl {
private static final long serialVersionUID = 5906427978349712224L;
private static Log log = LogFactory.getLog(DeleteEnversListener.class);
public DeleteEnversListener(AuditConfiguration enversConfiguration) {
super(enversConfiguration);
}
@Override
public void onPostDelete(PostDeleteEvent event) {
log.info("!!! just logging entity !! "+ event.getEntity());
super.onPostDelete(event);
}
}
以类似的方式,我有
第3步:创建自己的org.hibernate.integrator.spi.Integrator
实现
public class CustomEnversIntegrator extends EnversIntegrator {
private static Log log = LogFactory.getLog(CustomEnversIntegrator.class);
@Override
public void integrate(Configuration configuration,
SessionFactoryImplementor sessionFactory,
SessionFactoryServiceRegistry serviceRegistry) {
super.integrate(configuration, sessionFactory, serviceRegistry);
final AuditConfiguration enversConfiguration = AuditConfiguration.getFor( configuration, serviceRegistry.getService( ClassLoaderService.class ) );
EventListenerRegistry listenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );
System.out.println("Registering event listeners");
if (enversConfiguration.getEntCfg().hasAuditedEntities()) {
listenerRegistry.appendListeners(EventType.POST_INSERT, new InsertEnversListener(enversConfiguration));
listenerRegistry.appendListeners(EventType.POST_UPDATE, new UpdateEnversListener(enversConfiguration));
listenerRegistry.appendListeners(EventType.POST_DELETE, new DeleteEnversListener(enversConfiguration ) );
listenerRegistry.appendListeners(EventType.POST_COLLECTION_RECREATE, new CollectionRecreateEnversListener(enversConfiguration ) );
listenerRegistry.appendListeners(EventType.PRE_COLLECTION_REMOVE, new PreCollectionRemoveEnversListener(enversConfiguration ) );
listenerRegistry.appendListeners(EventType.PRE_COLLECTION_UPDATE, new PreCollectionUpdateEnversListener(enversConfiguration ) );
}
}
}
步骤4:为了在Hibernate启动时自动使用积分器,您需要添加一个META-INF/services/org.hibernate.integrator.spi.Integrator
文件。这是org.hibernate.integrator.spi.Integrator
文件的内容
com.hib.sample.listener.CustomEnversIntegrator
如果我遗失任何东西,我不确定。我正在使用JBOSS AS 7.0和Hibernate 4.1.8
这是一个仅限Spring的解决方案,用于条件Envers
审计,没有丑陋的META-INF
文件夹等。您需要的只是配置类中的bean和CustomEnversEventListener
。
@Bean
public EventListenerRegistry listenerRegistry(EntityManagerFactory entityManagerFactory) {
ServiceRegistryImplementor serviceRegistry = entityManagerFactory.unwrap(SessionFactoryImpl.class).getServiceRegistry();
final EnversService enversService = serviceRegistry.getService(EnversService.class);
EventListenerRegistry listenerRegistry = serviceRegistry.getService(EventListenerRegistry.class);
listenerRegistry.setListeners(EventType.POST_UPDATE, new CustomEnversEventListener(enversService));
return listenerRegistry;
}
和
public class CustomEnversEventListener extends EnversPostUpdateEventListenerImpl {
CustomEnversEventListener(EnversService enversService) {
super(enversService);
}
@Override
public void onPostUpdate(PostUpdateEvent event) {
// custom conditional stuff
super.onPostUpdate(event);
}
}
如果只想自定义一个侦听器,即EnversPostUpdateEventListener,则不需要禁用hibernate.listeners.envers.autoRegister
以便让Envers注册另一个侦听器。
然后你可以通过Envers
覆盖listenerRegistry.setListeners
监听器或者通过listenerRegistry.appendListeners
追加
尝试将积分器文件放入:
sample.war\WEB-INF\classes\META-INF\services\...
也许...
在我的情况下,我使用Maven,我必须在pom.xml
中包含以下行:<include>**/*.Integrator</include>
,因为该文件未包装在.ear
中。
我的pom.xml
:
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.Integrator</include>
</includes>
</resource>
...
在我的maven项目的文件夹META-INF / services / src / main / resources下创建一个文件org.hibernate.integrator.spi.Integrator(包含我的自定义集成器的限定名称)使我的自定义集成器代码被调用。
@ComponentScan(basePackages = {“com.example.demo”},lazyInit = true)
添加lazyInit = true,为我触发了自定义集成器。