Rika
Rika

Reputation: 796

What does it mean to be "associated with persistence context"?

In this documentation http://docs.oracle.com/javaee/5/tutorial/doc/bnbqw.html, it says

Managed entity instances have a persistent identity and are associated with a persistence context.Detached entity instances have a persistent identify and are not currently associated with a persistence context.

So I am trying to understand what "asscoiated with a persistence context mean"?

Upvotes: 2

Views: 1827

Answers (2)

wypieprz
wypieprz

Reputation: 8219

You can treat a Persistence Context as a container that has an ability to store entities that can be managed and synchronized with a database by using Entity Manager. The following diagram depicts dependencies between JPA classes and interfaces:

Relationships between JPA classes and interfaces


A process of association an entity with a persistence context can be realized by executing:

  • EntityManager.persist method on a newly created entity
  • EntityManager.merge method on a detached entity
  • EntityManager.find method with the given entity type and its primary key
  • EntityManager.createNativeQuery, EntityManager.createNamedQuery and EntityManager.createQuery methods with SQL / JPQL / CriteriaAPI based queries

In case of transaction-scoped container-managed persistence context these methods need to be invoked within the scope of a transaction.

@Entity
public class Employee {
    @Id
    private int id; //determines persistent identity
    public Employee(int id) { this.id = id; }
}

Persistent identity allows to locate the entity across all entities of the same type by its primary key and to synchronize to the database in case of flushing or committing the persistence context (either manually by flush/commit or automatically).

Employee emp = new Employee(1);

em.persist(emp);
boolean isManaged = em.contains(emp); //true, managed, id=1
boolean isFound = Objects.equals(emp, em.find(Employee.class, 1)); //true

Persisting the entity causes it to become managed (associated with persistence context). It dos not mean that the entity is synchronized to the database.


A process of disassociation an entity with a persistence context can be realized by executing:

  • EntityManager.detach or EntityManager.remove methods on a managed entity; the difference between them is that a detached entity remain in the underlying database since removed one is not
  • EntityManager.commit, EntityManager.rollback, EntityManager.clear, EntityManager.close methods which affects persistence context
  • serialization as an effect of passing it to another tier or sending through a remote interface
...
em.detach(emp);
isManaged = em.contains(emp); //false, detached, id=1
isFound = Objects.equals(emp, em.find(Employee.class, 1)); //false

Although detached entity still has assigned persistent identity it's no longer associated with the persistence context thus synchronized with the database. As a consequence any changes to a detached entity will not be persisted and committed unless it's merged into the persistence context. In such case it's possible that entity has been overridden by another party hence becoming stale, therefore id of entity being merged needs to be cleared/reassigned.

Upvotes: 4

Khary Mendez
Khary Mendez

Reputation: 1848

The persistence context is the code responsible for synchronizing the state of any attached entities with the state of the database. The EntityManager class is used to interact with the persistence context. Queries are executed via the EntityManager (which interacts with the persistence context) and keeps the entity state in sync with the database contents. When an entity is detached from the persistence context (for example by calling the detach method on the EntityManager) that entity's state is no longer kept in sync with the database.

Upvotes: 3

Related Questions