2017-04-07 37 views

回答

1

有在Hibernate中4和5的替代品,但現在看來似乎並不真的打算Hibernate的內部以外的地方使用。

您需要訪問SessionImplementor,因此您必須將會話轉換爲該會話。我找不到一種方法來獲取不涉及getCurrentSession()結果的訪問。然後,您可以使用TransactionCoordinator創建一個類似於Isolater的IsolationDelegate

SessionImplementor session = (SessionImplementor) sessionFactory.getCurrentSession(); 
    IsolationDelegate isolationDelegate = session.getTransactionCoordinator().createIsolationDelegate(); 
    isolationDelegate.delegateWork(new AbstractWork() { 
     @Override 
     public void execute(Connection connection) throws SQLException { 
      // This will run on a separate connection with the current 
      // transaction suspended (if necessary) 
     } 
    }, false); 
1

你可以做的一件事是使用Hibernate動作隊列來註冊一個特定的回調,該回調在提交之前立即觸發,或立即根據您的用例進行註冊。這些類:

org.hibernate.action.spi.BeforeTransactionCompletionProcess org.hibernate.action.spi.AfterTransactionCompletionProcess

爲您的使用情況下,似乎你想使用AfterTransactionCompletionProcess。爲了與特定的會話你會註冊回調:

session.getActionQueue().registerProcess( 
    new AfterTransactionCompletionProcess() { 
    @Override 
    void doAfterTransactionCompletion(
     boolean success, 
     SharedSessionContractImplementor session) { 
     // do your logic here 
    } 
    } 
); 
相關問題