PersistentObjectException: detached entity passed to persist (JPA/Hibernate)
Codemia Knowledge Hub ·
PersistentObjectException: detached entity passed to persist means you called EntityManager.persist() on an entity that already has a database identity (a non-null @Id). Use merge() instead of persist() for detached entities, or make sure the entity is truly new before persisting it.
Entity lifecycle states
Every JPA entity is in one of four states, and this exception comes from confusing two of them:
| State | Has @Id? | Managed by EntityManager? | In Database? |
| New / Transient | No (or null) | No | No |
| Managed / Persistent | Yes | Yes | Yes |
| Detached | Yes | No | Yes |
| Removed | Yes | Yes (scheduled for delete) | Yes (until flush) |
persist() expects a new entity with no database identity. If the entity has an @Id value and is not currently managed, JPA treats it as detached and throws:
Five common causes and their fixes
1. Persisting an entity from a closed session
This is the most frequent cause. You load an entity, the EntityManager closes or the transaction ends, and you then persist the same object in a new context:
Fix: call merge() instead:
2. Cascading persist to a detached parent
With CascadeType.PERSIST or CascadeType.ALL on a relationship, persisting the child cascades persist() to the parent. If the parent is detached, the cascade fails:
Fix: merge the parent first, or change the cascade type:
3. Setting the ID on a @GeneratedValue entity
If the entity uses @GeneratedValue but you set the ID yourself before persisting, Hibernate treats it as detached:
Fix: leave the @Id field unset when using @GeneratedValue and let Hibernate assign it.
4. Entities built from JSON or DTOs
When an entity arrives in a REST request body, JSON deserialization creates a new Java object with a populated ID that no persistence context manages:
Fix: use merge(), or find the managed entity and update it. Find-then-update is more explicit and gives you control over what gets modified:
5. Bidirectional relationships with only one side set
With bidirectional @OneToMany / @ManyToOne mappings, failing to update both sides can leave detached references:
Fix: always synchronize both sides:
persist() vs merge()
| Method | Input State | What It Does | Returns |
persist() | Transient (new) | Inserts a new row, assigns ID | void (original object becomes managed) |
merge() | Detached or transient | Copies state into a managed entity, inserts or updates | Managed copy (original stays detached) |
merge() returns a new managed instance and leaves the object you passed in detached. Always keep working with the returned object:
Spring Data JPA's save() picks for you
Spring Data JPA's save() decides internally whether to call persist() or merge():
It decides "new vs existing" by checking whether the @Id is null (for generated IDs), or through Persistable<ID> for assigned IDs:
If you use raw JPA without Spring, you have to choose between persist() and merge() yourself.
Mistakes that cause or hide the error
- Using
merge()everywhere "just to be safe":merge()always issues a SELECT before the INSERT or UPDATE to check for existing records. For batch inserts of new entities,persist()is significantly faster because it skips that check. CascadeType.ALLon@ManyToOne: this cascadespersist()to the parent, which is almost always already persisted. PutCascadeType.ALLon the@OneToManyside and be selective on@ManyToOne.- Forgetting
@GeneratedValue: without it, JPA does not generate IDs. An entity with a null@Idand no@GeneratedValuemay be treated differently depending on the provider.
Related reading
- pg.InternalError SSL SYSCALL error EOF detected
- PHP code to convert a MySQL query to CSV
- PHP date format when inserting into datetime in MySQL
- PHP MySQL Google Chart JSON - Complete Example
- Peterson algorithm in Java?
- Places where JavaBeans are used?
- PHP MySQL transactions examples
- php mysqli_connect authentication method unknown to the client caching_sha2_password
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.