Card 01/ 07
All 7 cards
ConceptDifficulty: Advanced1 min
Four States, One Object
new Employee("Alex") produces an object. Nothing about the database knows it exists yet, and setting a field on it is exactly as inert as setting a field on any other plain object — nothing is being tracked, nothing will be written anywhere.
A JPA entity is always in exactly one of four states, and which one decides whether touching it does anything at all.
| State | Means |
|---|---|
| Transient | Just an object — new, never persisted, not tracked |
| Managed (persistent) | The persistence context is tracking it; changes are picked up automatically |
| Detached | Was managed once; the persistence context has stopped watching it |
| Removed | Marked for deletion; the row disappears when the transaction commits |
So the question that actually matters is never "is this saved?" — it's "which of the four states is this object in right now?" The rest of this topic is the operations named on the diagram, and what each one actually does.