gpt4 book ai didi

java - Ho 究竟在 Spring 中使用 JPA 吗?一些疑惑

转载 作者:行者123 更新时间:2023-11-30 08:18:05 26 4
gpt4 key购买 nike

我正在学习 Spring Core 认证,我发现对一些与 Spring 中 JPA 使用相关的概念有疑问。

阅读培训类(class)文档,它谈到了 JPA EntityManager 接口(interface),它说它做了以下事情:

  • 管理工作单元和其中的持久对象:PersistenceContext

  • 生命周期通常绑定(bind)到事务(通常是容器管理的)

那么在这种情况下,工作单元到底是什么?

它是否类似于一组持久类,在其上定义了一个JPA 提供程序(如Hibernate)并且定义了一个< strong>事务类型(本地与 JTA)?或者什么?

所以我知道 EntityManager 接口(interface)在 OO 模型关系模型 之间提供了一个桥梁,使用以下代表一些常见的方法对数据库的操作:

  • persist(Object o):** 将实体添加到持久性上下文中:**SQL:插入表......

  • remove(Object o):** 从持久性上下文中删除实体:**SQL:从表中删除......

  • find(Class entity, Object primaryKey): ** 通过主键查找:**SQL: select * from table where id = ?

  • Query createQuery(String jpqlString):创建一个 JPQL 查询

  • flush():强制写入已更改的实体状态立即到数据库

等等

所以现在我对这个界面有些疑虑。这只是一个接口(interface),所以我需要它的具体实现才能工作。我认为我不必自己实现以前的方法,但我必须使用像 Hibernate 这样的 ORM 为我提供实现,所以我不需要做额外的工作。是否正确?

谢谢

最佳答案

Unit of Work

A unit of work is a design pattern described by Martin Fowler as “ [maintaining] a list of objects affected by a business transaction and coordinates the writing out of changes and the resolution of concurrency problems. ”[PoEAA] In other words, its a series of operations we wish to carry out against the database together. Basically, it is a transaction, though fulfilling a unit of work will often span multiple physical database transactions (see Section 11.1.2, “Long conversations”). So really we are talking about a more abstract notion of a transaction. The term "business transaction" is also sometimes used in lieu of unit of work.

(最好引用现有文档而不是重新排列单词,我认为它很清楚)

关于EntityManager,你是对的,它是每个 JPA 提供者实现的接口(interface),以及规范的其余部分(JPA 本身是接口(interface)的集合,如果没有执行)。同样,the documentation 的另一个好引述

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.

The set of entities that can be managed by a given EntityManager instance is defined by a persistence unit. A persistence unit defines the set of all classes that are related or grouped by the application, and which must be colocated in their mapping to a single database.

关于java - Ho 究竟在 Spring 中使用 JPA 吗?一些疑惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27797764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com