The EntityManager is the main interface of Jakarta Persistence (JPA) for working with entities. It owns a persistence context, a set of managed entity instances whose changes it tracks, and it is the API you use to persist, find, update, remove and query entities against a database. All changes are written to the database when the transaction commits or when you call flush().
In current applications (Spring Boot 3 and later, Hibernate 6 and later) the package is jakarta.persistence. Older code uses javax.persistence, which is the same API before the move to Jakarta EE.
An EntityManager instance is bound to one persistence context. Inside that context, each database row is represented by at most one managed Java object. This gives you three things:
find() twice for the same id in one context returns the same object.UPDATE at flush time without any explicit save call.You obtain an EntityManager from an EntityManagerFactory, which is created once per persistence unit and is thread-safe. The EntityManager itself is not thread-safe when you create it yourself. In Spring, you inject a shared proxy that is safe to use from many threads because it delegates to the transaction-bound instance.
The following repository shows the four basic operations with Spring and Jakarta Persistence. Constructor injection of EntityManager is supported by Spring Boot 3, so @PersistenceContext is optional.
The details that trip people up:
| Method | What it does | Common mistake |
|---|---|---|
persist(entity) | Makes a new entity managed and schedules an INSERT | Calling it on a detached entity throws PersistenceException |
find(Class, id) | Loads by primary key, returns null if missing | Expecting an exception when the row is absent |
getReference(Class, id) | Returns a lazy proxy without hitting the database | Touching it outside a transaction after the row was deleted |
merge(entity) | Copies state of a detached entity onto a managed one and returns that managed one | Continuing to use the argument instead of the returned object |
remove(entity) | Schedules a DELETE for a managed entity | Passing a detached instance |
Write operations need an active transaction. Without one, persist, merge and remove on a transaction-scoped EntityManager throw TransactionRequiredException.
Every entity instance is in one of four states, and the EntityManager moves it between them:
new, not associated with any context and no row yet.find() or passed to persist(). Changes are tracked.detach() or clear(). Changes are no longer tracked; use merge() to bring the state back.remove(). The row is deleted at flush.createQuery() runs JPQL against entities, and createNativeQuery() runs SQL against tables. Prefer the typed variant and named parameters.
Never concatenate user input into a query string; bound parameters protect against SQL injection. For dynamic queries you can use the Criteria API through em.getCriteriaBuilder().
The EntityManager also controls when and how the context synchronises with the database.
flush() sends pending SQL to the database without committing. Use it when you need generated values or constraint violations early.clear() detaches every managed entity. It is useful in long batch jobs to keep memory bounded.detach(entity) detaches a single entity.refresh(entity) reloads state from the database and overwrites unflushed changes.contains(entity) tells you whether an entity is managed.Spring Data JPA repositories use an EntityManager internally. Use JpaRepository for standard CRUD and derived queries, and inject the EntityManager directly when you need operations repositories do not expose, such as flush, detach, getReference, custom Criteria queries or entity graphs.
persist makes a new entity managed and the same instance becomes the managed one. merge copies the state of a detached or new instance into a managed instance and returns that copy, so you must use the returned value.
No, a plain EntityManager is not thread-safe, and the EntityManagerFactory is. In Spring, the injected EntityManager is a shared proxy that resolves to a transaction-bound instance per thread, so injecting it into singletons is safe.
Reads work without a transaction, but the loaded entities are detached when the call returns (unless an open-session-in-view setting keeps the context open). Writes always need a transaction.
A managed entity is dirty checked. When the transaction flushes, the provider compares current state with the snapshot taken at load time and issues an UPDATE for changes.
The EntityManager is the gateway to the persistence context: it manages entity state, tracks changes, runs JPQL and native queries, and controls flushing. Use persist, find, merge and remove inside transactions, remember that merge returns the managed copy, and keep the context small in bulk work with flush() and clear(). Use jakarta.persistence imports on current Spring Boot 3 and Hibernate 6 stacks.