The error can be caused by several things:
- Flushing the data before committing the object may lead to clear all object pending for persist.
- If object has primary key which is auto generated and you are forcing an assigned key
- if you are cleaning the object before committing the object to database.
- Zero or Incorrect ID: If you set the ID to zero or something else, Hibernate will try to update instead of insert.
- Object is Stale: Hibernate caches objects from the session. If the object was modified, and Hibernate doesn’t know about it, it will throw this exception — note the StaleStateException
I'm not taking the credit for it, found it here.