JPA EntityManager

EntityManager is the core JPA API for managing entity lifecycle and interacting with persistent data. It provides operations for finding, persisting, updating, and removing entities within a persistence context.

An `EntityManager` manages a persistence context, which is the set of entity instances currently tracked by JPA. When an entity is managed, JPA can detect changes to it and synchronize those changes with the database during flush or transaction commit.

Common operations include `persist()` for making a new entity managed, `find()` for retrieving an entity by primary key, `remove()` for marking a managed entity for deletion, and `merge()` for copying the state of a detached entity into a managed instance. `createQuery()` can be used to execute JPQL queries.

The persistence context provides identity and change tracking. If an entity with a particular primary key is already managed, `find()` can return that existing managed instance rather than creating another instance for the same database row within the same context.

EntityManager operations that modify persistent state normally participate in a transaction. Calling `persist()` does not necessarily execute an immediate SQL `INSERT`; SQL is commonly issued when the persistence context is flushed.

In typical Java applications, an `EntityManager` is obtained from a container or framework rather than manually created for every operation. In Spring-based applications, an injected EntityManager is commonly associated with a transaction-scoped persistence context.

Key points

Interview questions

What is an EntityManager in JPA?

EntityManager is the main JPA interface for working with persistent entities. It can create, retrieve, update, and remove entities and can execute JPQL queries. It also manages a persistence context that tracks entity instances. Changes to managed entities can therefore be synchronized with the database automatically.

What is the difference between persist() and merge()?

`persist()` is normally used when introducing a new entity into the persistence context. The passed instance becomes managed. `merge()` is commonly used when an entity is detached or otherwise not managed; its state is copied into a managed instance, which the method returns. The original object passed to `merge()` does not become the managed instance simply because merge was called.

What is a persistence context?

A persistence context is a collection of entity instances that JPA currently manages. JPA uses it for identity management and change tracking. When a managed entity is modified, the provider can detect the change and synchronize it with the database during flush. The persistence context also helps avoid multiple managed Java objects representing the same database identity within that context.

When does EntityManager actually execute SQL?

Calling an EntityManager method does not always mean SQL is executed immediately. JPA can defer database synchronization until the persistence context is flushed. A flush may happen explicitly or as part of transaction processing, depending on the operation and configuration. The exact SQL and timing can also depend on the JPA provider and database interaction.

Why is EntityManager usually used within a transaction?

Database state changes performed through EntityManager generally need a transaction so that the persistence provider can coordinate changes consistently with the database. The transaction also defines an important boundary for persistence-context synchronization and commit behavior. Without an appropriate transaction, modifying operations can fail or behave differently depending on the environment and operation. Read operations can have different transactional requirements depending on the application and persistence setup.