What is Persistence Context?

前端 未结 9 1796
难免孤独
难免孤独 2020-12-07 08:19

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 fo

相关标签:
9条回答
  • 2020-12-07 08:37
    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.
    0 讨论(0)
  • 2020-12-07 08:39

    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.

    0 讨论(0)
  • 2020-12-07 08:42

    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

    http://gerrydevstory.com/wp-content/uploads/2012/05/jpa-state-transtition.png

    0 讨论(0)
  • 2020-12-07 08:45

    "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

    0 讨论(0)
  • 2020-12-07 08:46

    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.

    0 讨论(0)
  • 2020-12-07 08:55

    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.

    0 讨论(0)
提交回复
热议问题