[java] What is Persistence Context?

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.

This question is related to java jpa orm persistence

The answer is


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.


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.


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)

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.


  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.

"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


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.


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


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to jpa

No converter found capable of converting from type to type How does spring.jpa.hibernate.ddl-auto property exactly work in Spring? Deserialize Java 8 LocalDateTime with JacksonMapper Error creating bean with name 'entityManagerFactory' defined in class path resource : Invocation of init method failed How to beautifully update a JPA entity in Spring Data? JPA Hibernate Persistence exception [PersistenceUnit: default] Unable to build Hibernate SessionFactory How to return a custom object from a Spring Data JPA GROUP BY query How to find distinct rows with field in list using JPA and Spring? What is this spring.jpa.open-in-view=true property in Spring Boot? Spring Data JPA and Exists query

Examples related to orm

How to select specific columns in laravel eloquent Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] How to query between two dates using Laravel and Eloquent? Laravel - Eloquent "Has", "With", "WhereHas" - What do they mean? How to Make Laravel Eloquent "IN" Query? How to auto generate migrations with Sequelize CLI from Sequelize models? How to fix org.hibernate.LazyInitializationException - could not initialize proxy - no Session Select the first 10 rows - Laravel Eloquent How to make join queries using Sequelize on Node.js What is Persistence Context?

Examples related to persistence

Java: JSON -> Protobuf & back conversion What is Persistence Context? Name attribute in @Entity and @Table The import javax.persistence cannot be resolved Confusion: @NotNull vs. @Column(nullable = false) with JPA and Hibernate Correct use of flush() in JPA/Hibernate When to use EntityManager.find() vs EntityManager.getReference() with JPA No Persistence provider for EntityManager named Setting a JPA timestamp column to be generated by the database? Hibernate: "Field 'id' doesn't have a default value"