The EntityManager is the central interface of JPA. It manages a persistence context, the set of entity instances the provider is tracking, and it is the API you use to persist, find, update, remove and query entities. Changes you make to managed entities are detected automatically and written to the database when the transaction flushes or commits.
The interface is jakarta.persistence.EntityManager in current releases (javax.persistence before Jakarta EE 9). In Spring and Jakarta EE applications you normally receive a container-managed instance instead of creating one yourself.
An EntityManager combines four jobs:
persist, find, merge, remove, refresh, detach.Hibernate's Session is the provider-specific equivalent and extends EntityManager. Call entityManager.unwrap(Session.class) when you need Hibernate-only features.
The persistence context guarantees one instance per database row inside the same context, so two find calls with the same identifier return the same Java object. It also takes a snapshot of each managed entity. At flush time the provider compares the current state to the snapshot and issues UPDATE statements for differences, which is called dirty checking.
Because the entity is managed, no explicit update call is needed. The same applies to Spring Data's save on an existing managed entity, which is only needed for detached or new instances.
| State | Meaning | How to reach it |
|---|---|---|
| New (transient) | Created with new, unknown to the provider | new Employee(...) |
| Managed | Attached to the persistence context, changes tracked | persist, find, merge result, query result |
| Detached | Was managed, the context is closed or the entity cleared | Transaction ends, detach, clear |
| Removed | Scheduled for deletion at flush | remove |
find returns null when no row exists; getReference returns a proxy and throws EntityNotFoundException on first access if missing.merge copies state onto a managed instance and returns it. Use the returned object, not the argument.persist on a detached instance throws an exception.Always use named parameters, never string concatenation, to avoid SQL injection. Before executing a query, Hibernate flushes pending changes by default (flush mode AUTO), so queries see your uncommitted changes within the transaction.
An EntityManager is not thread safe, and one instance is tied to one unit of work. There are two models:
@PersistenceContext. It binds to the current transaction and is safe to inject into singletons.EntityManagerFactory, begin and commit transactions yourself, and close it. This is the model in plain Java SE.The EntityManager and EntityManagerFactory implement AutoCloseable since JPA 3.1, so try-with-resources works. In a Spring Data repository, JpaRepository methods wrap an EntityManager for you.
Hibernate's Session implements EntityManager and adds provider-specific operations. Code written against EntityManager stays portable.
At flush: on commit, on an explicit flush(), or before a query when flush mode is AUTO. Calling persist does not necessarily run the insert immediately, except with identity-generated ids, which need it to obtain the key.
No. Use one per unit of work. A Spring-injected @PersistenceContext proxy is safe because it resolves the real instance per transaction.
persist makes a new instance managed. merge copies a possibly detached instance onto a managed one and returns that managed copy.
The EntityManager is the gateway to JPA: it owns the persistence context, tracks entity state through the lifecycle, runs queries and coordinates with transactions. Know the four lifecycle states, rely on dirty checking inside transactions, use merge return values, and never share an instance between threads.