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

Ho exactly works JPA in Spring? Some doubts

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

阅读培训课程文档,它谈到了 JPA EntityManager 接口,它说它做了以下事情:

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

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

所以我知道 EntityManager 接口在 OO 模型 关系模型[= 之间提供了一个桥梁65=] 使用以下表示数据库上一些常见操作的方法:

  • persist(Object o): ** 将实体添加到持久性上下文中: **SQL: insert into table …

  • remove(Object o): ** 从持久性上下文中删除实体: **SQL: delete from table …

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

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

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

等等

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

Tnx

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 提供程序实现的接口,以及规范的其余部分(JPA 本身是接口的集合,没有实现就无法做任何事情).再次引用 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.