A JPA entity is always in one of four states: new (also called transient), managed (also called persistent), detached or removed. The state depends on whether the entity has an identity and whether it is attached to a persistence context, which is the first-level cache that an EntityManager uses to track changes. Moving an entity between states is what triggers inserts, updates and deletes.
This article explains each state, shows the methods that move an entity between them, and points out the mistakes that cause LazyInitializationException and unexpected SQL. The examples use the jakarta.persistence package (JPA 3.x, used by Hibernate 6 and Spring Boot 3).
The persistence context holds the entities an EntityManager currently manages, at most one instance per identity. Anything in it is tracked: when the transaction flushes, JPA compares each managed entity with its loaded snapshot and issues SQL for the differences. This is called dirty checking.
| State | In persistence context | Has database row | Changes saved automatically |
|---|---|---|---|
| New (transient) | No | No | No |
| Managed (persistent) | Yes | Yes, or will on flush | Yes |
| Detached | No | Yes | No |
| Removed | Yes, scheduled for deletion | Deleted on flush | Delete is executed |
The JPA specification names the states new, managed, detached and removed. Hibernate's own documentation uses transient, persistent, detached and removed, so you will see both vocabularies.
An entity is new when you create it with new and it is not associated with any persistence context. It has no row in the database, and JPA does not know about it.
Setting fields on a new entity produces no SQL. It becomes managed only when you pass it to persist().
An entity is managed when it belongs to an open persistence context. You get managed entities by calling persist() on a new entity, or by loading one with find(), getReference() or a query.
After persist(), the entity is managed, and any later change before flush is included automatically. You do not call a save method for each change; the update statement is generated at flush time. persist() may assign the identifier right away or at flush, depending on the generation strategy (IDENTITY forces an immediate insert).
An entity is detached when it has an identity but its persistence context is gone. This happens when the EntityManager is closed or cleared, when you call detach(), or when a transaction-scoped context ends (for example when a Spring service method returns).
To save changes made to a detached entity, call merge().
merge() copies the state onto a managed instance and returns it. The object you passed in stays detached, so continue with the returned reference. Detached entities are also the source of LazyInitializationException: reading an uninitialised lazy association after the context is closed fails because there is no session to load it.
An entity becomes removed when you call remove() on a managed entity. It stays in the persistence context until flush, when JPA issues the DELETE.
You cannot call remove() on a new entity (it is ignored) and, for a detached entity, you must load or merge it first. If you call persist() on a removed entity before the flush, it becomes managed again.
| Method | Effect |
|---|---|
persist(entity) | New to managed |
find(...), getReference(...), queries | Return managed entities |
merge(entity) | Detached or new to a managed copy |
remove(entity) | Managed to removed |
detach(entity) | Managed to detached |
clear() | All managed entities become detached |
close() | Context ends; all entities become detached |
refresh(entity) | Reloads state from the database, discarding changes |
flush() | Writes pending changes without changing states |
In Spring Boot you rarely call EntityManager directly. A @Transactional service method opens a persistence context for the duration of the transaction, so entities loaded inside it are managed and modified fields are saved at commit even without calling save().
repository.save() calls persist() for a new entity and merge() for an existing one, which is why it works for both inserts and updates. Outside a transaction, returned entities are detached, and Spring Boot's open-in-view setting (on by default for web apps) keeps the context open for the request, which can hide lazy loading problems until you disable it.
persist() makes a new entity managed and the same instance is tracked. merge() copies state onto a managed instance and returns that copy, which is what you use for detached entities.
You accessed an uninitialised lazy association after the entity became detached. Fetch the data inside the transaction, use a fetch join or an entity graph, or map to a DTO.
No. It checks whether the entity is new (by id or version) and calls persist() or merge(), so existing entities lead to an update.
No. Changes to a managed entity are detected and flushed at commit.
A JPA entity is new, managed, detached or removed. persist() and queries produce managed entities, closing or clearing the context detaches them, merge() reattaches state through a managed copy, and remove() schedules a delete. Understanding which state an entity is in explains when SQL runs, why changes are sometimes lost, and where lazy loading errors come from.