有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

显示为活动事务的java Hibernate错误

我有以下代码:

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中存在


共 (1) 个答案

  1. # 1 楼答案

    它一定是被重构了。因为您使用的是新的jar版本,所以无法找到它。你可以试试这个代码-

    if(TransactionSynchronizationManager.isActualTransactionActive()){ enter code here

    }否则{

    未处于活动状态
    }