maa*_*nus 15 java hibernate transactions listener
我想知道是否有可能找出hibernate 对数据库的真正作用(即提交的更改).我想就某些变化通知另一个流程.
我猜测,事件类型小号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(....);
Run Code Online (Sandbox Code Playgroud)
我要求它作为它的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 ");
}
}
Run Code Online (Sandbox Code Playgroud)
在事件系统可以另外被使用,或作为替换,用于拦截器.
1个 .Implement AfterTransactionCompletionProcess从org.hibernate.action封装接口并实现以下方法.文件
void doAfterTransactionCompletion(boolean success, SessionImplementor session) {
//Perform whatever processing is encapsulated here after completion of the transaction.
}
Run Code Online (Sandbox Code Playgroud)
否则,您可以使用EntityDeleteAction扩展CustomDeleteAction类并覆盖上面的doAfterTransactionCompletion方法.文件
2.通过实现PostDeleteEventListener并使用EventType.POST_COMMIT_DELETEpost post删除.
通过实现PostInsertEventListener并使用EventType.POST_COMMIT_INSERTfor post insert.
通过实现PostUpdateEventListener并使用EventType.POST_COMMIT_UPDATEpost更新.
以下是PostDeleteEventListener
,PostUpdateEventListener和PostInsertEventListener的几个示例.
该Object entityPostInsertEvent的给出了与数据库操作的实体.
该Object[] statePostInsertEvent的返回此事件的会话事件源.这是生成此事件的基础会话.
下面的链接包含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 );
}
}
Run Code Online (Sandbox Code Playgroud)
因此,监听器对事件的注册取决于实现细节.