英文:
What is JPA's EntityManager?
问题
Oracle的文档通过"持久性上下文"的概念来解释javax.persistence.EntityManager
。
用于与持久性上下文交互的接口。
EntityManager实例与持久性上下文相关联。持久性上下文是一组实体实例,其中对于任何持久性实体标识,都存在唯一的实体实例。在持久性上下文中,管理实体实例及其生命周期。EntityManager API用于创建和删除持久性实体实例,通过其主键查找实体,以及查询实体。
尽管在这里解释了"持久性上下文",但仍然不太清楚EntityManager
解决了什么问题,以及作为开发人员如何从中受益。
是否有一个更简单的入门解释,适用于对JPA世界新手的人?
**更新:**我收到的答案非常有帮助,但我和其他一些人可能有的重复问题是,JPA的概念是通过与其他JPA概念的关系来解释的。在不考虑JPA生态系统的情况下,以与其他ORM(例如Entity Framework或SQL Alchemy)的背景相同的方式介绍EntityManager
将非常有启发性。
英文:
Oracle's documentation is explaining javax.persistence.EntityManager
through the concept of "persistence context".
> Interface used to interact with the persistence context.
>
> An EntityManager instance is associated with a persistence context. A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. Within the persistence context, the entity instances and their lifecycle are managed. The EntityManager API is used to create and remove persistent entity instances, to find entities by their primary key, and to query over entities.
While "persistence context" is explained here, it still makes little sense what problem does EntityManager
solve and how I as a developer can benefit from using it.
Is there a simpler introductory explanation for people who are new to the world of JPA?
Update: the answers I received are incredibly helpful but the repeating issue I have (and some others might too) is that JPA concepts are explained through relationship with other JPA concepts. It would be super enlightening to get introduced to EntityManager
in terms agnostic to JPA ecosystem, say as if a person is coming with background in other ORMs (Entity Framework or SQL Alchemy, for example).
答案1
得分: 4
以下是已翻译的部分:
"在任何给定时间,同一实体管理器内的受管实体实例集合称为其持久性上下文。"
"在同一持久性标识内,同一持久性上下文内只能存在一个Java实例。"
"如果将此放在关系图上,它会看起来像:"
" 持久性"
" | 1 "
" | 创建"
" | *"
" 实体管理器工厂"
" | 1"
" | 创建"
" | *"
" 实体管理器"
" | * "
" | 管理"
" | 1"
" 持久性上下文"
"此外,EntityManager
及其相关的PersistenceContext
被称为所谓的一级缓存。第二级缓存由EntityManagerFactory
管理。"
英文:
You could paraphrase that to:
Set of managed entity instances within the same Entity Manager at any given time is called its Persistence Context.
And only one Java instance within the same Persistence Identity can exist in a Persistence Context at any given time.
If you put this on a relationship diagram it would look something like:
Persistence
| 1
| creates
| *
EntityManagerFactory
| 1
| creates
| *
EntityManager
| *
| manages
| 1
PersistenceContext
Also the EntityManager
and its related PersistenceContext
are the so-called first-level cache. The second-level cache would be managed by the EntityManagerFactory
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论