我有一個要求,只需要在狀態字段更改時審計記錄。我遵循了文檔章節教程「15.8。有條件的審計」。有條件的Envers審計
步驟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);
}
}
以類似的方式,我有
- InsertEnversListener
- UpdateEnversListener
- DeleteEnversListener
- CollectionRecreateEnversListener
- PreCollectionRemoveEnversListener
- PreCollectionUpdateEnversL istener
第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
目前發生了什麼? 您的定製集成商代碼是否已被調用? – adamw
@adamw:我已經將sysout語句添加到集成器代碼中,但沒有一個出現。看來服務不被JBOSS認可。我已經使用Eclipse 4.2 Juno作爲Dynamic Web項目創建了項目。 **不確定META-INF的位置是否正確**。任何建議.. –
你檢查了生成的jar/war,如果它有正確的地方的文件? – adamw