Hibernate transaction not successfully started
I got to know that this is already solved; even though I am posting my answer here.
I haven't found wasCommitted()
method on the transaction.
But the following code worked for me:
// commit only, if tx still hasn't been committed yet by Hibernate
if (tx.getStatus().equals(TransactionStatus.ACTIVE)) {
tx.commit();
}
This is a really old question and I figure you've already solved it (or given up on Hibernate) but the answer is tragically simple. I'm surprised no one else picked it up.
You haven't done a session.save(o), so there is nothing in the transaction to commit. The commit may still not work if you haven't changed anything in the object, but why would you want to save it if nothing has changed?
BTW: It is also perfectly acceptable to do the session.get(...) before the session.beginTransaction().
Well, it looks like once we reach the tx.commit()
line, the transaction has already been committed. My only guess is that Hibernate already commits the transaction when get()
ing the object.
The fix for this is simple:
// commit only if tx still hasn't been committed yet (by hibernate)
if (!tx.wasCommitted())
tx.commit();