PersistentObjectException detached entity passed to persist thrown by JPA and Hibernate
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
When working with Java Persistence API (JPA) and Hibernate, one common issue that developers often encounter is the PersistentObjectException: detached entity passed to persist. This particular exception can be a source of confusion and frustration, especially for those unfamiliar with the intricacies of entity states in ORM (Object-Relational Mapping) frameworks. Understanding what this exception means and how to resolve it is crucial for effective data persistence handling in Java applications using JPA or Hibernate.
Understanding PersistentObjectException
The exception PersistentObjectException: detached entity passed to persist primarily indicates that an object, which is in a detached state, is being re-submitted to the persistence context using the persist() method. To delve deeper into this, we need to understand the different states an entity can be in:
- Transient: The entity has just been created and not yet associated with any persistence context. It doesn't have any representation in the database nor an identifier value.
- Persistent: The entity is associated with a persistence context, meaning it is being managed by an EntityManager in JPA or a Session in Hibernate. Any changes made to this entity will be tracked and reflected in the database upon transaction completion.
- Detached: The entity was previously persistent (managed) but is no longer associated with a persistence context. This usually occurs when the EntityManager or Session is closed or the entity is explicitly evicted.
- Removed: The entity is scheduled for deletion from the database.
The error happens when an entity that is in a detached state is passed to the persist() method, which is intended for newly created (transient) entities that do not yet have a database identity. JPA or Hibernate throws this exception because it expects entities passed to persist() not to have a database identity yet.
When and Why This Happens
This situation typically arises in scenarios such as:
- When an entity is fetched in one session (and hence becomes managed), then the session is closed making the entity detached. Later, the same entity is attempted to be persisted again in a new session.
- Using serialization/deserialization which can detach an entity from its session.
- Manual cloning or copying of entity instances.
How to Resolve the Issue
To fix the PersistentObjectException: detached entity passed to persist, you need to re-attach the detached entity to the current persistence context or simply avoid using persist() on an entity that already has a database identity. Here are the correct methods to handle different states:
- merge(): This method should be used for detached entities. It will check if an entity with the same identifier exists in the current context and/or the database. If it does, it updates the existing entity; if it does not, it creates a new entry.
- refresh(): This method can re-sync the current state of the entity with the database, useful if you are unsure if the local entity is up-to-date.
- persist(): As mentioned, persist should only be used with entirely new, transient entities.
Here is a practical example demonstrating a correct handling scenario:
Summary Table
| Entity State | Description | Method to Use |
| Transient | Newly created, not yet persisted | persist() |
| Persistent | Managed by EntityManager, synchronized with DB | flush(), commit(), auto |
| Detached | No longer managed by EntityManager | merge() |
| Removed | Scheduled for deletion | Not applicable, must reattach and then remove if needed |
Conclusion
Understanding entity states and their transitions are key to effectively managing data persistence and avoiding common pitfalls like PersistentObjectException. By using the appropriate lifecycle methods (persist(), merge(), etc.), developers can ensure that their applications handle data correctly, enhancing both performance and reliability.

