Mike
Mike

Reputation: 2557

What is JPA's EntityManager?

Oracle's documentation is explaining javax.persistence.EntityManager through the concept of "persistence context".

Interface used to interact with the persistence context.

An EntityManager instance is associated with a persistence context. A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. Within the persistence context, the entity instances and their lifecycle are managed. The EntityManager API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities.

While "persistence context" is explained here, it still makes little sense what problem does EntityManager solve and how I as a developer can benefit from using it.

Is there a simpler introductory explanation for people who are new to the world of JPA?

Update: the answers I received are incredibly helpful but the repeating issue I have (and some others might too) is that JPA concepts are explained through relationship with other JPA concepts. It would be super enlightening to get introduced to EntityManager in terms agnostic to JPA ecosystem, say as if a person is coming with background in other ORMs (Entity Framework or SQL Alchemy, for example).

Upvotes: 2

Views: 1684

Answers (1)

Maciej Kowalski
Maciej Kowalski

Reputation: 26522

You could paraphrase that to:

Set of managed entity instances within the same Entity Manager at any given time is called its Persistence Context.

And only one Java instance within the same Persistence Identity can exist in a Persistence Context at any given time.

If you put this on a relationship diagram it would look something like:

Persistence
     | 1 
     | creates
     | *
EntityManagerFactory
     | 1
     | creates
     | *
EntityManager
     | * 
     | manages
     | 1
PersistenceContext

Also the EntityManager and its related PersistenceContext are the so-called first-level cache. The second-level cache would be managed by the EntityManagerFactory.

Upvotes: 4

Related Questions