You are here because you have just thrown out by your application with this annoying exception : javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist. There are multiple reasons that could cause this exception. But, basic idea behind this exception is that the object which you are trying to persist is not in the persistant state. Note that this exception will be thrown even if you dont have the transaction enabled in your application.
One of the complicated issue while working with Hibernate / JPA projects are that differentiating the state of persistent objects. If you no longer required an object, programatically one can detach an entity from the transaction and it will be no longer in the persistence state.
In many cases, the object would have been considered as detached even though you are not intentionally done that.
- Check if you are trying to persist an entity which has the same id as another entity, and which is already present in the PersistenceContext in your application.
- Do not set an ID before you save or persist it. Hibernate will look at the Entity you’ve passed and it assumes that because it has its Primary Key populated that it is already in the database.
- If you are working with Spring MVC application, check that whether you have added <tx:annotation-driven/> in your configuration file. If you are missing this line, transactions would not have been started in your application. This will ultimately throw exception javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist.
If you are still not able to resolve this issue, please write it in the comments section.