java – Hibernate事务没有成功启动

考虑这个简单的Hibernate场景:

session = getHibernateSession();
tx = session.beginTransaction();
SomeObject o = (SomeObject) session.get(SomeObject.class, objectId);
tx.commit();

此代码产生以下异常:

org.hibernate.TransactionException: Transaction not successfully started
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:100)
    at com.bigco.package.Clazz.getSomeData(Clazz.java:1234)

这是怎么回事?

那么看来,一旦我们到达了tx.commit()行,事务已经被提交了。我唯一的猜测是,当get()对象时,Hibernate已经提交了事务。

这个修复很简单:

// commit only if tx still hasn't been committed yet (by hibernate)
if (!tx.wasCommitted())
    tx.commit();
翻译自:https://stackoverflow.com/questions/5155039/hibernate-transaction-not-successfully-started

转载注明原文:java – Hibernate事务没有成功启动