2017-04-07 41 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 
    } 
    } 
); 
相关问题