Hibernate 拦截器和事件监听器 [英] Hibernate interceptor and event listeners

查看:20
本文介绍了Hibernate 拦截器和事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能找出休眠真正对数据库做了什么(即,提交的更改).我想就某些更改通知另一个进程.

I wonder if it's possible to find out what hibernate really did to the database (i.e., committed changes). I'd like to notify another process on certain changes.

我猜EventTypes POST_COMMIT_DELETEPOST_COMMIT_UPDATEPOST_COMMIT_INSERT 应该可以,但鉴于完全零文档,这只是一个猜测.有人可以确认吗?我缺什么吗?

I guess that the EventTypes POST_COMMIT_DELETE, POST_COMMIT_UPDATE and POST_COMMIT_INSERT should do, but given exactly zero documentation, it's only a guess. Can someone confirm? Am I missing any?

我也不确定如何获得真正写入的内容.PostInsertEvent 包含 Object entityObject[] state,我应该信任这两者中的哪一个?

I'm also unsure about how to obtain what gets really written. The PostInsertEvent contains both Object entity and Object[] state, which of the two should I trust?

一个附带问题:我没有使用 XML、没有 Spring、没有 JPA,只有 ConfigurationbuildSessionFactory.这真的是监听器的注册方式吗?

A side question: I'm using no XML, no Spring, no JPA, just Configuration and buildSessionFactory. Is this really the way listeners should be registered?

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

我问的是 1. 依赖于实现细节,2 完全丑陋,3. 几乎完全无法发现.

I'm asking as its 1. dependent on an implementation detail, 2 totally ugly, 3. nearly perfectly undiscoverable.

推荐答案

,可以在提交 DB 中的某些更改后通知另一个进程(例如:审核).那就是在使用Hibernate的自定义拦截器事件提交JDBC事务(Hibernate wraps JDBC transaction)之后立即做某些事情.

Yes, it is possible to notify another process(For Example: Auditing) after committing certain changes in the DB. That is to do certain things right after the JDBC transaction(Hibernate wraps JDBC transaction ) is committed using Custom Interceptors and Events of Hibernate.

您可以通过使用 EmptyInterceptor 休眠类对其进行扩展来创建您自己的自定义拦截器类.并通过覆盖 EmptyInterceptor 的以下 afterTransactionCompletion(Transaction tx) 方法在事务提交后执行某些任务.

You can create your own custom interceptor class by extending it by EmptyInterceptor class of hibernate. And by overriding the below afterTransactionCompletion(Transaction tx) method of EmptyInterceptor to do certain tasks after the transaction has been committed.

public class AuditLogInterceptor extends EmptyInterceptor {
 @Override
 public void afterTransactionCompletion(Transaction tx) {
    System.out.println("Task to do after transaction ");
 }
}

<小时>

事件系统可以作为拦截器的补充或替代.


The Event system can be used in addition, or as a replacement, for interceptors.

1.从 org.hibernate.action 包中实现 AfterTransactionCompletionProcess 接口并实现以下方法.文档

1.Implement AfterTransactionCompletionProcess interface from org.hibernate.action package and implement the below method. documentation

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

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

Otherwise, you can extend your CustomDeleteAction class with EntityDeleteAction and override the above doAfterTransactionCompletion method. documentation

2.通过实现 PostDeleteEventListener 并使用 EventType.POST_COMMIT_DELETE 进行后期删除.
通过实现 PostInsertEventListener 并使用 EventType.POST_COMMIT_INSERT 进行后期插入.
通过实现 PostUpdateEventListener 并使用 EventType.POST_COMMIT_UPDATE 进行后期更新.
以下是 PostDeleteEventListener<的几个例子/a>, PostUpdateEventListenerPostInsertEventListener.

2.By Implementing PostDeleteEventListener and using EventType.POST_COMMIT_DELETEfor post delete.
By Implementing PostInsertEventListener and using EventType.POST_COMMIT_INSERTfor post insert.
By Implementing PostUpdateEventListener and using EventType.POST_COMMIT_UPDATEfor post update.
Here are few examples of PostDeleteEventListener , PostUpdateEventListener and PostInsertEventListener.

PostInsertEvent 的Object entity 给出了参与数据库操作的实体.

The Object entity of PostInsertEvent gives the entity involved in the database operation.

PostInsertEvent 的Object[] state 返回此事件的会话事件源.这是生成此事件的基础会话.
以下链接包含 PostInsertEvent 成员的文档.

The Object[] state of PostInsertEvent returns the session event source for this event. This is the underlying session from which this event was generated.
Below link contains the documentation for PostInsertEvent Members.

http://mausch.github.io/nhibernate-3.2.0GA/html/6deb23c7-79ef-9599-6cfd-6f45572f6894.htm

注册事件监听器:下面 MyIntegrator 类显示了3 种方法来注册事件监听器.

Registering event listeners: Below MyIntegrator class shows 3 ways to register event listeners.

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

因此,侦听器注册到事件取决于实现细节.

Hence, Listeners registration to a event is dependent on implementation detail.

这篇关于Hibernate 拦截器和事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆