Koray Tugay
Koray Tugay

Reputation: 23844

What exactly is a Persistence in JPA? How is it connected to EntityManagerFactory?

This figure is from the book called PRO JPA :

Relationships between JPA Concepts

And the text below this figure is:

Figure 2-1 shows that for each persistence unit there is an EntityManagerFactory and that many entity managers can be created from a single EntityManagerFactory.

So the Persistence class has a static method called createEntityManagerFactory, where you provide a String which is the persistenceUnitName.

1) Does that mean that all the EntityManagers are identically same if they are created with the same persistenceUnitName ?

As far as I know, persistenceUnitName is just the persistence.xml we have in the application, which has some configurations like driver name, username, password, and entity class names.

2) Then what is the Persistence Unit seen in this figure? And how does it configure the EntityManagerFactory?

3) And, what is a PersistenceContext, and how does a Persistence Unit create one, and how is it managed by the EntityManager? Is it created as required by the Persistence Unit itself?

Upvotes: 2

Views: 649

Answers (1)

Gab
Gab

Reputation: 8332

See my answer to another of your questions :

https://stackoverflow.com/a/16844778/2087640. If different EntityManager instances share the same persistence unit, each one will have its own in-memory representation of the same database schema state.

Upvotes: 2

Related Questions