2016-01-20 30 views
-1

我有這樣的代碼:休眠誤差節目是事務是活動的

import org.hibernate.Query; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.Configuration; 
... 
public void commitTransaction() throws PersistenceException { 
    Transaction t = (Transaction) transaction.get(); 
    if (t == null) { 
     throw new PersistenceException("Pas de transaction ouverte"); 
    } else if (!t.isActive()) { 
     throw new PersistenceException("Transaction déjà fermée"); 
    } 
    t.commit(); 
    transaction.set(null); 
    closeSession(); 
} 

但我在此行中的錯誤:

...} else if (!t.isActive()) {... 

消息錯誤:

The method isActive() is undefined for the type Transaction

我使用hibernate-core-5.0.7.Final.jar

當我看在interface Transaction有沒有函數isActive()爲什麼在老版本的hibernate-core ..中存在?

回答

-2

它必須被重構出來。由於使用新的jar版本你是不是能夠找到it.You可使用此代碼嘗試 -

如果(TransactionSynchronizationManager.isActualTransactionActive()){ enter code here

}其他{

不活動
}

+1

'TransactionSynchronizationManager'用於Spring .. ?? – Mercer

+0

我看你正在尋找冬眠。您將不得不使用會話對象來查詢session.getTransaction()。isActive()。 –