简体   繁体   English

何先生在春季完全可以使用JPA? 一些疑问

[英]Ho exactly works JPA in Spring? Some doubts

I am studying for the Spring Core certification and I am finding some doubts about some concept related to the JPA use in Spring. 我正在学习Spring Core认证,并且对与Spring中使用JPA有关的某些概念有一些疑问。

Reading the training course documentation it speack about the JPA EntityManager interface and it say that it do the following things: 阅读培训课程文档时,它会特别关注JPA EntityManager接口,并说它会执行以下操作:

  • Manages a unit of work and persistent objects therein: the PersistenceContext 管理工作单元和其中的持久对象:PersistenceContext

  • Lifecycle often bound to a Transaction (usually containermanaged) 生命周期通常绑定到事务(通常是容器管理的)

So what exactly is a unit of work in this context? 那么在这种情况下,工作单元到底是什么?

Is it something like a group of persistent classes on which is definied a JPA provider (like Hibernate ) and it is definied a transactional types (local vs. JTA)? 它是否像一组持久化类,在其上定义了JPA提供程序 (如Hibernate ),并定义了事务类型 (本地与JTA)? Or what? 要不然是啥?

So I know that the EntityManager interface provide a bridje between the OO model and the relational model using the following method that represent some common operation on a DB: 因此,我知道EntityManager接口使用以下表示数据库上一些常见操作的方法在OO模型关系模型之间建立了桥梁:

  • persist(Object o): ** Adds the entity to the Persistence Context: **SQL: insert into table … persist(Object o):**将实体添加到持久性上下文中:** SQL:插入表中……

  • remove(Object o): ** Removes the entity from the Persistence Context: **SQL: delete from table … remove(Object o):**从持久性上下文中删除实体:** SQL:从表中删除...

  • find(Class entity, Object primaryKey): ** Find by primary key: **SQL: select * from table where id = ? find(类实体,对象primaryKey):**通过主键查找:** SQL:从表中选择* id =?

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

  • flush(): Force changed entity state to be written to database immediately flush():强制将更改后的实体状态立即写入数据库

etcetc etcetc

So now I have some dount about this interface. 所以现在我对这个界面有些痴迷了。 This is only an interface so I need a concrete implementation of it to work. 这只是一个接口,因此我需要对其进行具体实现。 I think that I have not to implement the previous method by myself but I have to use an ORM like Hibernate that provide me an implementation so I don't need to do extrawork. 我认为我不必自己实现以前的方法,而必须使用像Hibernate这样的ORM,它可以为我提供实现,因此我不需要做额外的工作。 Is it correct? 这是正确的吗?

Tnx 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. 工作单元是一种设计模式,由Martin Fowler描述为“ [维护]受业务交易影响的对象列表,并协调更改的写出和并发问题的解决。 ”[PoEAA] In other words, its a series of operations we wish to carry out against the database together. “ [PoEAA]换句话说,我们希望对数据库一起执行它的一系列操作。 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”). 基本上,这是一个事务,尽管完成一个工作单元通常会跨越多个物理数据库事务(请参见第11.1.2节“长时间对话”)。 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. 有时也使用术语“业务交易”代替工作单位。

(Better to quote existing documentation instead of rearranging the words, I think it is pretty clear) (更好地引用现有文档而不是重新排列单词,我认为这很清楚)

And you are right about EntityManager , it is a interface that each JPA provider implements, along with the rest of the spec (JPA itself is a collection of interfaces, not able to do anything without an implementation). 您对EntityManager是正确的,它是每个JPA提供程序都实现的接口,以及其余规范(JPA本身是接口的集合,如果没有实现,则无法执行任何操作)。 Again, another fine quote of the documentation 再次, 该文档的另一个很好的报价

An EntityManager instance is associated with a persistence context. EntityManager实例与持久性上下文关联。 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. EntityManager API用于创建和删除持久实体实例,通过其主键查找实体以及查询实体。

The set of entities that can be managed by a given EntityManager instance is defined by a persistence unit. 可以由给定EntityManager实例管理的实体集由持久性单元定义。 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. 持久性单元定义了与应用程序相关或分组的所有类的集合,并且所有类在映射到单个数据库时必须共置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM