簡體   English   中英

Hibernate攔截器和事件偵聽器

[英]Hibernate interceptor and event listeners

我想知道是否有可能找出hibernate對數據庫的真正作用 (即提交的更改)。 我想就某些變化通知另一個流程。

我想EventTypePOST_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進行更新。
以下是PostDeleteEventListenerPostUpdateEventListenerPostInsertEventListener的幾個示例。


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

因此,監聽器對事件的注冊取決於實現細節。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM