Quick revision
The short answer
An entity is new (transient) when we have just created it with new and no persistence context knows it; managed when a persistence context tracks it, which is what persist, find and queries give us; detached when it was managed but the context was closed or cleared, or detach was called; and removed when remove was called on a managed entity, so it will be deleted at flush. merge is the bridge back: it copies the state of a detached or new entity into a managed instance and returns that instance.
What to remember
In this answer, we saw the four JPA entity states. A new entity is just a Java object, a managed one is tracked and its changes are written at flush, a detached one was managed but its context ended or it was cleared or detached, and a removed one is scheduled for deletion at flush. persist turns new into managed, find and queries return managed entities, remove schedules a delete, and merge copies a detached or new object's state onto a managed instance and returns that instance, leaving the argument unchanged.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
An entity is new (transient) when we have just created it with new and no persistence context knows it; managed when a persistence context tracks it, which is what persist, find and queries give us; detached when it was managed but the context was closed or cleared, or detach was called; and removed when remove was called on a managed entity, so it will be deleted at flush. merge is the bridge back: it copies the state of a detached or new entity into a managed instance and returns that instance.
Why this question is asked
Almost every confusing JPA error, from 'detached entity passed to persist' to changes that are never saved, comes down to not knowing which state an entity is in. Interviewers ask about the four states because they are the mental model behind dirty checking, lazy loading and merge. In this answer, we will recap what a persistence context is, walk through each state with code that shows how we enter and leave it, and summarise every transition in one table.
1A Quick Recap of the Persistence Context
A persistence context is the set of entity objects that one EntityManager is currently tracking. While an object is tracked, JPA watches it: at flush time, it compares the object with the state it was loaded in and writes any changes to the database automatically. In Spring, a persistence context usually lives exactly as long as a transaction.
The four states describe an object's relationship with a persistence context. The question is always the same: is this object being tracked right now, and if not, has it ever been, or is it scheduled for deletion?
2New: Created but Not Tracked
When we create an entity with new, it is just a Java object. No persistence context knows about it, it usually has no id yet, and nothing we do to it reaches the database. JPA calls this state new, and it is often called transient.
3Managed: Tracked by the Persistence Context
An entity becomes managed when we call persist on a new entity, or when it is returned by find, getReference or a query. A managed entity is in the persistence context, and every change to it will be written at the next flush, without calling any save method.
Notice that we never called update. That is the main benefit of the managed state: the persistence context writes changes for us, and it also guarantees that within one context each row is represented by exactly one object.
4Detached: Was Managed, Not Any More
An entity becomes detached when its persistence context ends while the object lives on. That happens when the context is closed, which in Spring usually means the transaction ended; when we call clear(), which detaches everything; or when we call detach(entity) for one object. A detached entity still has its id and data, but nobody is watching it.
This is the state behind many surprises: changes that silently vanish, and lazy loading errors after the transaction is over. To save a detached entity's changes, we must bring its state back into a persistence context, which is what merge does.
5Removed: Scheduled for Deletion
Calling remove on a managed entity moves it to the removed state. It is still in the persistence context until flush, when JPA sends the DELETE. After commit, the object is effectively a new-looking object with a stale id. We cannot call remove on a detached entity directly; the specification says it throws IllegalArgumentException, so we first find or merge it.
6Merge: Bringing State Back
merge takes an object that is detached or new, finds or creates the managed instance with the same id, copies the object's state onto it, and returns the managed instance. The object we passed in stays as it was; it does not become managed.
| From | Operation | To |
|---|---|---|
| New | persist(e) | Managed (INSERT at flush) |
| New or detached | merge(e) | A managed copy is returned; e is unchanged |
| (database row) | find, getReference, query | Managed |
| Managed | commit / close, clear(), detach(e) | Detached |
| Managed | remove(e) | Removed (DELETE at flush) |
| Removed | persist(e) before flush | Managed again |
| Detached | remove(e) | IllegalArgumentException |
So after merge, we must continue working with the returned object, not the one we passed in.
7Follow-up questions
Is an entity managed after Spring Data's save() returns?
It depends on whether it was new. For a new entity, save() calls persist, and the same object becomes managed. For an entity with an id, save() calls merge and returns the managed copy, which is a different object from the one we passed in. That is why we should always use the return value of save().
Does flush change an entity's state?
No. Flush writes pending changes to the database inside the transaction, but managed entities stay managed and removed entities are gone from the context afterwards. The transaction can still roll back after a flush, undoing the written changes.
Can a managed entity belong to two persistence contexts?
No. Each persistence context has its own objects. If two transactions load the same row, each gets its own instance, managed by its own context. That is why comparing entities from different transactions with == is always false.
8Conclusion
In this answer, we saw the four JPA entity states. A new entity is just a Java object, a managed one is tracked and its changes are written at flush, a detached one was managed but its context ended or it was cleared or detached, and a removed one is scheduled for deletion at flush. persist turns new into managed, find and queries return managed entities, remove schedules a delete, and merge copies a detached or new object's state onto a managed instance and returns that instance, leaving the argument unchanged.
Open with
JPA entities have four states relative to a persistence context, which is the set of objects one EntityManager is tracking. New, also called transient, is an object I just created with new; it has no id and nothing about it reaches the database.
Then, if they want more
Managed means the persistence context tracks it: I get there with persist on a new entity, or by loading with find, getReference or a query. Changes to a managed entity are written automatically at flush through dirty checking, and the context guarantees one object per row.
Detached means it was managed, but the context is gone: in Spring the transaction ended, or someone called clear or detach. It keeps its data and id, but changes are not tracked and lazy associations throw LazyInitializationException. Removed means I called remove on a managed entity, and the DELETE runs at flush.
To get from detached back to managed I use merge: it loads or finds the managed instance with the same id, copies the detached state onto it and returns that instance, while the object I passed stays detached. So after merge, or after Spring Data's save on an existing entity, I always continue with the returned object.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Describe the four JPA entity states and which EntityManager operations move an entity...": https://scrivio-interview-library.netlify.app/databases/jpa-fundamentals/3d687a3d40/