我有一个要求,我只想在状态字段更改时审核记录。我遵循了文档章节教程“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);
}
}
以类似的方式,我有
- 插入环境监听器
- 更新环境监听器
- 删除EnversListener
- CollectionRecreateEnversListener
- PreCollectionRemoveEnversListener
- PreCollectionUpdateEnversListener
第 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