Java 什么是持久化上下文?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19930152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:37:09  来源:igfitidea点击:

What is Persistence Context?

javajpaormpersistence

提问by Amrit

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.

我是 Java 世界和 JPA 的新手。我在学习 JPA 时遇到了许多新术语,例如实体、持久性。在阅读时,我无法理解Persistence Context的确切定义。

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

谁能用简单的外行术语解释一下?它与 中使用的数据有@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.

持久性上下文是一组实体,对于任何持久性身份,都有一个唯一的实体实例。

采纳答案by JamesB

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:

虽然与 Hibernate 相关(一个 JPA 提供者),但我认为这些链接很有用:

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

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

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.

在 Java EE 中,持久化上下文通常通过 EntityManager 访问。

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

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://docs.jboss.org/hibernate/entitymanager/3.6/reference/en/html/objectstate.html

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

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

回答by PLP

Both the org.hibernate.SessionAPI and javax.persistence.EntityManagerAPI represent a context for dealing with persistent data.

这两个org.hibernate.SessionAPI及javax.persistence.EntityManagerAPI代表了处理持久性数据的上下文。

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

这个概念称为持久性上下文。持久数据具有与持久上下文和底层数据库相关的状态。

回答by pritam kumar

  1. Entities are managed by javax.persistence.EntityManagerinstance using persistence context.
  2. Each EntityManagerinstance is associated with apersistence context.
  3. Within the persistence context, the entity instances and their lifecycle are managed.
  4. Persistence contextdefines a scopeunder which particularentity instances are created, persisted, and removed.
  5. A persistence contextis like a cachewhich contains a set of persistent entities, So once the transaction is finished, all persistent objects are detached from the EntityManager's persistence contextand are no longer managed.
  1. 实体由javax.persistence.EntityManager实例使用持久化上下文管理。
  2. 每个EntityManager实例都与一个持久化上下文相关联。
  3. 持久化上下文中,实体实例及其生命周期受到管理。
  4. 持久化上下文定义了一个范围,在该范围内创建、持久化和删除特定实体实例。
  5. 一个持久化上下文就像一个缓存,其中包含一组持久化实体,因此一旦事务完成,所有持久化对象都从EntityManager 的持久化上下文中分离,不再被管理。

回答by thisismydesign

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

虽然@pritam kumar 给出了一个很好的概述,但第五点是不正确的。

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

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

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

JPA 的 EntityManager 和 Hibernate 的 Session 提供了一个扩展的持久化上下文。

回答by qwerty

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

一个持续的背景下代表持有的数据和有资格像一个数据库中的一些持久性存储要永久保存的实体。一旦我们commit在具有这些实体的会话下创建了一个事务,Hibernate 就会刷新持久上下文,并且对它们的更改(插入/保存、更新或删除)将持久存储在持久存储中。

回答by Koray Tugay

Taken from thispage:

取自页面:

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

这是 JPA 世界的快速备忘单:

  • 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)
  • 缓存是数据的副本,副本的意思是从数据库中提取但存在于数据库之外。
  • 刷新缓存是将修改后的数据放回数据库的行为。
  • PersistenceContext 本质上是一个缓存。它还倾向于拥有自己的非共享数据库连接。
  • EntityManager 代表一个 PersistenceContext(因此也代表一个缓存)
  • EntityManagerFactory 创建一个 EntityManager(因此也创建一个 PersistenceContext/Cache)

回答by Dhanushka

"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.

JPA @Entity 注解表示一个可持久化的实体。

Refer JPA Definition here

在此处参考 JPA定义

回答by AggarwalM

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

通俗地说,我们可以说 Persistence Context 是一个管理实体的环境,即将“实体”与数据库同步。