141

I am new to the Java world and JPA. I was studying JPA and came across many new terms like Entity, persistence. While reading, I could not understand the exact definition for Persistence Context.

Can anyone explain it in simple laymen terms? What is it to do with the data used in the @Entity?

For example, I find this definition too complicated to understand:

A persistence context is a set of entities such that for any persistent identity there is a unique entity instance.

M.A.R.
  • 159
  • 1
  • 7
  • 14
Amrit
  • 2,295
  • 4
  • 25
  • 42

10 Answers10

104

A persistence context handles a set of entities which hold data to be persisted in some persistence store (e.g. a database). In particular, the context is aware of the different states an entity can have (e.g. managed, detached) in relation to both the context and the underlying persistence store.

Although Hibernate-related (a JPA provider), I think these links are useful:

http://docs.jboss.org/hibernate/core/4.0/devguide/en-US/html/ch03.html

http://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/architecture.html

In Java EE, a persistence context is normally accessed via an EntityManager.

http://docs.oracle.com/javaee/6/api/javax/persistence/EntityManager.html

The various states an entity can have and the transitions between these are described below:

http://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/objectstate.html

https://vladmihalcea.com/wp-content/uploads/2014/07/jpaentitystates.png

Denis
  • 759
  • 1
  • 9
  • 22
JamesB
  • 7,774
  • 2
  • 22
  • 21
  • hmmm, it makes sense now. Do you have a similar "Simple" definition which tells the difference between container-managed & application-managed Entity Managers? – Amrit Nov 12 '13 at 13:35
  • 6
    container vs application just basically tells where Entity Manager is created - outside in a container, or inside, in application. – uiron Nov 12 '13 at 14:34
88
  1. Entities are managed by javax.persistence.EntityManager instance using persistence context.
  2. Each EntityManager instance is associated with a persistence context.
  3. Within the persistence context, the entity instances and their lifecycle are managed.
  4. Persistence context defines a scope under which particular entity instances are created, persisted, and removed.
  5. A persistence context is like a cache which contains a set of persistent entities , So once the transaction is finished, all persistent objects are detached from the EntityManager's persistence context and are no longer managed.
pritam kumar
  • 1,040
  • 7
  • 9
  • 1
    I found that EclipseLink does not detach entities after a transaction is finished... – Ray Hulha Sep 01 '15 at 10:11
  • The container managed persistence context's cache will only remain for the duration of the transaction. Entities read in a transaction will become detached after the completion of the transaction and will require to be merged to be edited in subsequent transactions. [EclipseLink](https://eclipse.org/eclipselink/documentation/2.4/concepts/cache001.htm). @RayHulha – pritam kumar Sep 08 '15 at 04:55
  • 6
    @pritamkumar, you explained well the concept of a persistence context. I'd just add that there's also the javax.persistence.PersistenceContext annotation, which is used to inject an EntityManager object and establish the scope of the injected object (e.g., a transaction). – Paulo Merson Feb 12 '16 at 19:13
  • so why isnt this an implementation detail and why do I have to manually specify my EntityManager to use a PersistenceContext ? –  Sep 24 '21 at 07:25
51

Taken from this page:

Here's a quick cheat sheet of the JPA world:

  • A Cache is a copy of data, copy meaning pulled from but living outside the database.
  • Flushing a Cache is the act of putting modified data back into the database.
  • A PersistenceContext is essentially a Cache. It also tends to have it's own non-shared database connection.
  • An EntityManager represents a PersistenceContext (and therefore a Cache)
  • An EntityManagerFactory creates an EntityManager (and therefore a PersistenceContext/Cache)
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
  • can one PersisnteceContext have any EntityManager instances? And can one Entity Manager have any PersistenceContext? PersisntenceContext is only one for all application? – Roberto Feb 06 '19 at 03:57
10

Both the org.hibernate.Session API and javax.persistence.EntityManager API represent a context for dealing with persistent data.

This concept is called a persistence context. Persistent data has a state in relation to both a persistence context and the underlying database.

CodeSlave
  • 425
  • 6
  • 21
PLP
  • 101
  • 1
  • 2
10

A persistent context represents the entities which hold data and are qualified to be persisted in some persistent storage like a database. Once we commit a transaction under a session which has these entities attached with, Hibernate flushes the persistent context and changes(insert/save, update or delete) on them are persisted in the persistent storage.

qwerty
  • 2,392
  • 3
  • 30
  • 55
7

In layman terms we can say that Persistence Context is an environment where entities are managed, i.e it syncs "Entity" with the database.

AggarwalM
  • 71
  • 1
  • 1
6

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. @Entity is nothing but a class which helps us create objects in order to communicate with the database.And the way the objects communicate is using methods.And who supplies those methods is the Entity Manager.

neomatrix
  • 59
  • 1
  • 2
5

"A set of persist-able (entity) instances managed by an entity manager instance at a given time" is called persistence context.

JPA @Entity annotation indicates a persist-able entity.

Refer JPA Definition here

Dhanushka
  • 216
  • 3
  • 7
4

While @pritam kumar gives a good overview the 5th point is not true.

Persistence Context can be either Transaction Scoped-- the Persistence Context 'lives' for the length of the transaction, or Extended-- the Persistence Context spans multiple transactions.

https://blogs.oracle.com/carolmcdonald/entry/jpa_caching

JPA's EntityManager and Hibernate's Session offer an extended Persistence Context.

thisismydesign
  • 21,553
  • 9
  • 123
  • 126
  • Hi user2771889, You are right I didn't mention the extended scope of persistence context. 5th point was just related to transaction scoped persistence context. – pritam kumar Oct 03 '16 at 18:45
0

The persistence context is the first-level cache where all the entities are fetched from the database or saved to the database.

AtomicLamb
  • 11
  • 1