Hibernate拦截器和事件监听器

我想知道是否有可能找出hibernate对数据库的真正作用 (即提交的更改)。 我想就某些变化通知另一个流程。

我想EventType的POST_COMMIT_DELETEPOST_COMMIT_UPDATEPOST_COMMIT_INSERT应该这样做,但是只给出零文档,这只是猜测。 有人可以证实吗? 我错过了吗?

我也不确定如何获得真正的书面内容。 PostInsertEvent包含Object entityObject[] state ,我应该信任哪两个?


一个附带问题:我没有使用XML,没有Spring,没有JPA,只有ConfigurationbuildSessionFactory 。 这真的是听众应该注册的方式吗?

  EventListenerRegistry registry = ((SessionFactoryImpl) sessionFactory) .getServiceRegistry() .getService(EventListenerRegistry.class); registry.appendListeners(....); 

我要求它作为它的1.依赖于实现细节,2完全丑陋,3几乎完全不可发现。

是的 ,在提交数据库中的某些更改后,可以通知另一个进程(例如:审计)。 这是在使用自定义拦截器和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接口并实现以下方法。 文件

 void doAfterTransactionCompletion(boolean success, SessionImplementor session) { //Perform whatever processing is encapsulated here after completion of the transaction. } 

否则,您可以使用EntityDeleteAction扩展CustomDeleteAction类并覆盖上面的doAfterTransactionCompletion方法。 文件

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 ); } } 

因此,监听器对事件的注册取决于实现细节。