从 Hibernate 4.3.1 切换到 5.0.6 并且事务消失了

Switched from Hibernate 4.3.1 to 5.0.6 and Transaction is gone

我正在开发 Hibernate project.I 使用过 Netbeans 的 Hibernate 4.3.1 库。然后我需要使用 Apache Lucene 进行全文搜索。为了能够使用 Lucene 我需要切换到 Hibernate 5.x jars.I 可以定义一个新的 Transaction 对象但是 wasRollecBack 方法 Transaction class不管用。我在几个地方使用了这种方法,现在我被卡住了。当我查看 Hibernate 5.0.6 的 javadoc 时,没有像 org.hibernate.transaction 这样的东西。有org.hibernate.engine.transaction,但也不行。

当我回到 4.3.1 时 wasRolledBack 正在工作,但这次我不能 运行 使用 lucene 库进行项目。我很迷惑。

wasRolledBack 方法未包含在 Hibernate 5.0.6 版本 Transaction 接口 Here 中。

4.3.1 版本发生在 wasRolledBack 方法中。

现有方法:

public interface Transaction {

    void begin();

    void commit();

    void rollback();

    TransactionStatus getStatus();

    void registerSynchronization(Synchronization synchronization) throws HibernateException;

    void setTimeout(int seconds);

    int getTimeout();

    void markRollbackOnly();

 }

我没测试过,不过你可以用getStatus的方法。

示例:

    TransactionStatus transactionStatus = session.getTransaction().getStatus();
    if(transactionStatus.equals(TransactionStatus.ROLLED_BACK)){
        //action s.a :)
    }

编辑 1:

TransactionStatus枚举常量和说明:

ACTIVE : The transaction has been begun, but not yet completed.

COMMITTED : The transaction has been competed successfully.

COMMITTING :Status code indicating a transaction that has begun the second phase of the two-phase commit protocol, but not yet completed this phase.

FAILED_COMMIT:The transaction attempted to commit, but failed.

MARKED_ROLLBACK:The transaction has been marked for rollback only.

NOT_ACTIVE:The transaction has not yet been begun

ROLLED_BACK:The transaction has been rolled back.

ROLLING_BACK:Status code indicating a transaction that is in the process of rolling back.