Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PersistenceUnit vs PersistenceContext

In few project I have been successfully using

@PersistenceUnit(unitName = "MiddlewareJPA")
EntityManagerFactory emf;
...
EntityManager entityManager = emf.createEntityManager();

to obtain EntityManager for Database connection, but some days ago I was trying to move my project to Jboss EAP 6.2 and it couldn't create EntityManager. I was googling it and I found that I should try change @PersistenceUnit to

@PersistenceContext(unitName = "MiddlewareJPA")
private EntityManager entityManager;

to obtain EntityManager. It worked but I don't know why. What is the difference bettween PersistenceUnit and PersistenceContext? What are pros and cons of each one? Where should we be using one of them?

like image 468
user2377971 Avatar asked Jan 10 '14 07:01

user2377971


People also ask

What does PersistenceContext annotation do?

Persistence Context is an environment or cache where entity instances(which are capable of holding data and thereby having the ability to be persisted in a database) are managed by Entity Manager.It syncs the entity with database. All objects having @Entity annotation are capable of being persisted.

What is PersistenceContext?

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.

What is EntityManager and EntityManagerFactory?

The EntityManager performs database crud, create re-update delete operations allowing it to persist, update, retrieve or remove objects from a database. The persistence unit creates one or more EntityManagerFactory objects. Each EntityManagerFactory is configured by one persistence unit.

What is @PersistenceContext in spring boot?

The @PersistenceContext annotation in your code is being used to indicate that the EntityManager must be automatically injected, in other words its lifecycle will be managed by the container running your application (which is a good thing).


3 Answers

PersistenceUnit injects an EntityManagerFactory, and PersistenceContext injects an EntityManager. It's generally better to use PersistenceContext unless you really need to manage the EntityManager lifecycle manually.

like image 169
chrylis -cautiouslyoptimistic- Avatar answered Oct 11 '22 20:10

chrylis -cautiouslyoptimistic-


I don't know how it works exactly in the Java EE, but in Spring, when you specify @PersistenceContext annotation, it injects EntityManager. Where does it get EntityManager? It is wrong to create one EntityManager for the whole application lifetime by calling EntityManagerFactory.createEntityManager(). So instead a special implementation of EntityManager interface is used and instantiated directly. It has an internal mutable thread-local reference to a real EntityManager. Implementations of methods just redirect calls to this real EntityManager. And there is a servlet listener, that before each request obtain EM by calling EMF.createEntityManager() and assign it to that inner reference of special EM. Also this listener manages transactions by calling getTransaction().begin(), .commit() and .rollback() on the real EM. It is very simplified description of performed work. And I believe, that JEE container does the same thing, as Spring does.

In general case it is better to inject EntityManager, because with EntityManagerFactory and @PersistenceUnit you should create/destroy EntityManager every time by hands and manage transactions too.

like image 44
Alexey Andreev Avatar answered Oct 11 '22 19:10

Alexey Andreev


EntityManager obtained via @PersistenceContext is called Container Managed EntityManager as container will be responsible for managing "EntityManager". EntityManager obtained via @PersistenceUnit / entityManagerFactory.createEntityManager() is managed in the application by the developer. (for e.g. managing lifecycle of EntityManager, releasing the resources acquired by EntityManager, etc.).

like image 17
Raj Avatar answered Oct 11 '22 21:10

Raj