I believe you get this error when the objects in the Hibernate Session don't match the database.
This can happen due to multiple threads accessing the same objects, database rollbacks etc.
For example, I can cause this error as follows:
1) Let Hibernate transaction time out after 'saving' objects. This causes an exception. Database is rolled back but session still thinks the object has been updated.
2) Do an update on the same objects. Hibernate assumes that the object is in the database and throws the error when it doesn't find it.
To solve the problem, I changed to use merge instead of update. This commits your changes without assuming that the database and session are in line. Alternatively, you could try catching the exception, closing and clearing the session and retrying the transaction.
|