我想知道是否有可能找出 hibernate 真正 对数据库做了什么(即提交的更改)。我想就某些更改通知另一个进程。
我猜 EventType s 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(....);
我问的是它 1. 取决于实现细节,2 完全丑陋,3. 几乎完全无法被发现。
最佳答案
是,在数据库中提交某些更改后可以通知另一个进程(例如:审计)。即使用Hibernate的Custom Interceptors和Events,在JDBC transaction(Hibernate wraps JDBC transaction)提交后立即做一些事情。
您可以通过使用 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 ");
}
}
Event 系统可以作为拦截器的补充或替代品。
下面列出了在事务事件完成/提交后执行某些任务的几种方法
1.从org.hibernate.action包中实现AfterTransactionCompletionProcess接口(interface)并实现下面的方法。 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实体
给出了参与数据库操作的实体。
PostInsertEvent 的Object[] 状态
返回此事件的 session 事件源。这是生成此事件的基础 session 。
下面的链接包含 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 );
}
}
因此,事件的监听器注册取决于实现细节。
关于java - Hibernate 拦截器和事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39501074/