我想知道是否有可能找出hibernate 真正对数据库做了什么(即提交的更改)。我想就某些变化通知另一个流程。
我想EventType T2
POST_COMMIT_DELETE
,POST_COMMIT_UPDATE
和POST_COMMIT_INSERT
应该这样做,但只提供零文档,这只是猜测。有人可以证实吗?我错过了吗?
我也不确定如何获得真正写的东西。 PostInsertEvent
包含Object entity
和Object[] state
,我应该相信哪两个?
一个附带问题:我没有使用XML,没有Spring,没有JPA,只有Configuration
和buildSessionFactory
。这真的是听众应该注册的方式吗?
EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory)
.getServiceRegistry()
.getService(EventListenerRegistry.class);
registry.appendListeners(....);
我要求它依赖于实现细节,2完全丑陋,3几乎完全不可发现。
答案 0 :(得分:7)
是,在提交数据库中的某些更改后,可以通知另一个进程(例如:审核)。这是在使用Hibernate的自定义拦截器和事件提交JDBC事务(Hibernate包装JDBC事务)之后立即执行某些操作。
您可以通过hibernate的 EmptyInterceptor 类扩展它来创建自己的自定义拦截器类。并且通过覆盖下面的EmptyInterceptor的afterTransactionCompletion(Transaction tx)方法,在事务提交后执行某些任务。
public class AuditLogInterceptor extends EmptyInterceptor {
@Override
public void afterTransactionCompletion(Transaction tx) {
System.out.println("Task to do after transaction ");
}
}
事件系统可以作为拦截器的补充或替代使用。
1 。从org.hibernate.action包中实现 AfterTransactionCompletionProcess 接口并实现以下方法。 documentation
void doAfterTransactionCompletion(boolean success, SessionImplementor session) {
//Perform whatever processing is encapsulated here after completion of the transaction.
}
否则,您可以使用 EntityDeleteAction 扩展CustomDeleteAction类,并覆盖上面的doAfterTransactionCompletion方法。 documentation
2 。通过实施 PostDeleteEventListener 并使用EventType.POST_COMMIT_DELETE
进行删除。
通过实施 PostInsertEventListener 并使用EventType.POST_COMMIT_INSERT
进行帖子插入。
通过实施 PostUpdateEventListener 并使用EventType.POST_COMMIT_UPDATE
进行更新。
以下是PostDeleteEventListener的几个示例
,PostUpdateEventListener和PostInsertEventListener。
PostInsertEvent的Object entity
给出了数据库操作中涉及的实体。
PostInsertEvent的Object[] state
返回此事件的会话事件源。这是生成此事件的基础会话
下面的链接包含PostInsertEvent成员的文档。
http://mausch.github.io/nhibernate-3.2.0GA/html/6deb23c7-79ef-9599-6cfd-6f45572f6894.htm
注册事件监听器:MyIntegrator类下面显示了 3种方式来注册事件监听器。
public class MyIntegrator implements org.hibernate.integrator.spi.Integrator {
public void integrate(Configuration configuration, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
// As you might expect, an EventListenerRegistry is the thing with which event listeners are registered
// It is a service so we look it up using the service registry
final EventListenerRegistry eventListenerRegistry = serviceRegistry.getService( EventListenerRegistry.class );
// If you wish to have custom determination and handling of "duplicate" listeners, you would have to add an
// implementation of the org.hibernate.event.service.spi.DuplicationStrategy contract like this
eventListenerRegistry.addDuplicationStrategy( myDuplicationStrategy );
// EventListenerRegistry defines 3 ways to register listeners:
// 1) This form overrides any existing registrations with
eventListenerRegistry.setListeners( EventType.AUTO_FLUSH, myCompleteSetOfListeners );
// 2) This form adds the specified listener(s) to the beginning of the listener chain
eventListenerRegistry.prependListeners( EventType.AUTO_FLUSH, myListenersToBeCalledFirst );
// 3) This form adds the specified listener(s) to the end of the listener chain
eventListenerRegistry.appendListeners( EventType.AUTO_FLUSH, myListenersToBeCalledLast );
}
}
因此,监听者对事件的注册取决于实施细节。