简体   繁体   English

JPA 的 commit() 方法是否使实体分离?

[英]Does JPA's commit() method make entity detached?

I have been searching JPA entity life cycle nowadays.我现在一直在搜索 JPA 实体生命周期。 But now , there are some missing points about entity life cyle.但是现在,关于实体生命周期还有一些缺失的地方。 I have found following graphic in one of stackoverflow posts and keep in mind this diagram had been upvoted.我在 stackoverflow 的一篇帖子中发现了以下图形,请记住,此图已被点赞。

JPA 实体生命周期

According to this diagram , when we persist entity it becomes managed .根据这个图,当我们持久化实体时,它就变成了托管的。 OK .好的 。 No problem .没问题 。 When we commit , data goes to database.当我们提交时,数据进入数据库。 OK .好的 。 No problem.没问题。 But diagram shows us this commit operation made entity detached !但是图表向我们展示了这个提交操作使实体分离! Let's look at below psuedo code.让我们看看下面的伪代码。

entityManager.persist(entity);
transaction.commit(); // action completed and entity has become detached.(According to the diagram.)
entityManager.remove(entity); //Attention this step please .

in previous step(commit step).在上一步(提交步骤)中。 So how is it possible to remove a detached object?那么如何移除一个分离的对象呢? If this entity becomes detached , we all know it is not possible to manage a detached entity since it has no assocaiton with persistence context anymore.如果这个实体变得 detached ,我们都知道不可能管理一个 detached 实体,因为它不再与持久化上下文相关联。

So how is it possible to remove a detached object?那么如何移除一个分离的对象呢? Could you please clarify me at this point ?你能在这一点上澄清我吗? Thanks in advance !提前致谢 !

Entity can become detached in one of the following ways (there could be more ways):实体可以通过以下方式之一分离(可能有更多方式):

  1. When the transaction (in transaction-scoped persistence context) commits, entities managed by the persistence context become detached.当事务(在事务范围的持久性上下文中)提交时,持久性上下文管理的实体变得分离。

  2. If an application-managed persistence context is closed, all managed entities become detached.如果应用程序管理的持久性上下文关闭,则所有管理的实体都将分离。

  3. Using clear method使用清除方法

  4. using detach method使用分离方法

  5. rollback回滚

  6. In extended persistence context when a stateful bean is removed, all managed entities become detached.在扩展持久性上下文中,当有状态 bean 被移除时,所有受管实体都将分离。

I think the problem could be the difference between application managed, user managed, extended persistence contexts.我认为问题可能在于应用程序管理、用户管理、扩展持久性上下文之间的差异。

This picture is from openjpa but IMO (other opinions are welcome) its a bit wrong?!这张图片来自 openjpa 但 IMO(欢迎其他意见)它有点错误?! (In EE with TRANSACTION Scope its okay) But in a JAVA SE Example like this: .... (在带有 TRANSACTION Scope 的 EE 中)但是在这样的 JAVA SE 示例中:....

EntityTransaction et = em.getTransaction();
et.begin();
em.persist(entity);
et.commit();
System.out.println(em.contains(entity)); // true
em.detach(entity);
System.out.println(em.contains(entity)); // false
entity = em.merge(entity);
System.out.println(em.contains(entity)); // true

The entity is detached after detached method.实体在分离方法后分离。 After commit it stays in the entity persistence context.提交后,它停留在实体持久化上下文中。

For your question: when you have a detached object you can use merge to reattach it to the persistence context.对于您的问题:当您有一个分离的对象时,您可以使用合并将其重新附加到持久性上下文。

2 things : the state remove and detached are different : Removed means that the entity is still managed and will trigger a deletion in persitence layer on the flush, Detached means that the entity is no longer managed and that the changes made on it won't be reported to database.两件事:状态 remove 和 detached 是不同的: Removed表示实体仍受管理,并将在刷新时触发持久层中的删除, Detached表示不再管理实体,并且不会对其进行更改报告给数据库。

Your entity state is related to an entityManager .您的实体状态与entityManager相关。 Managed means that the EM tracks all changes made on it and will report them on database at flush time. Managed意味着EM跟踪对其所做的所有更改,并将在刷新时将它们报告到数据库中。

You must understand that reporting changes to the database has no sense outside of a transaction ( JPA support only transacted access to DB and only in isolation level READ_COMMITED ).您必须了解,在事务之外报告对数据库的更改没有任何意义( JPA仅支持对 DB 的事务性访问,并且仅在隔离级别READ_COMMITED )。

Tracking change on the entity once the transaction in which it has been retrieved has expired has so no sense as EntityManager won't be able to alter database state outside of a transaction.一旦检索到它的事务过期,跟踪实体上的更改就没有意义,因为EntityManager将无法在事务之外更改数据库状态。

That's why The EntityManager in JPA is designed to be created for each unit of work (contrary to the persistenceUnit, ie. the entityManagerFactory which is created once for the whole application).这就是为什么JPAEntityManager被设计为为每个工作单元创建(与persistenceUnit 相反,即为整个应用程序创建一次的 entityManagerFactory)。

In consequence the EntityManager should have the same scope than the transaction and should be released just after the commit (which is the case when you let the container manage the entityManager lifecycle for you).因此EntityManager应该具有与事务相同的范围,并且应该在提交后立即释放(当您让容器为您管理 entityManager 生命周期时就是这种情况)。

That's also the reason why JPA does not support nested transactions.这也是JPA不支持嵌套事务的原因。

JPA Spec: JPA规范:

3.3 Persistence Context Lifetime and Synchronization Type
...
An EntityManager with an extended persistence context maintains its references to the entity objects
after a transaction has committed. Those objects remain managed by the EntityManager, and they can
be updated as managed objects between transactions.
...
3.3.2 Transaction Commit
The managed entities of a transaction-scoped persistence context become detached when the transaction
commits; the managed entities of an extended persistence context remain managed.

Note that an application-managed entity manager (which is what you will typically use in Java SE) always uses an extended persistence context.请注意,应用程序管理的实体管理器(这是您通常在 Java SE 中使用的)始终使用扩展的持久性上下文。

So the short answer to your question is: when using a transaction-scoped PC, commit will cause detachment;所以对你的问题的简短回答是:当使用事务范围的 PC 时,提交会导致分离; when using an extended PC, commit will NOT cause detachment.使用扩展 PC 时,提交不会导致分离。

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

相关问题 为什么实体对象在方法返回后会被分离? (弹簧数据 JPA) - Why does entity object will be detached, after a method returns it? (Spring data JPA) Spring JPA,CrudRepository 的 save 方法是否立即提交到 DB? - Spring JPA, does CrudRepository's save method commit to DB immediately? 带有集合的JPA实体对分离成员上的contains方法返回false - JPA entity with collection returns false for contains method on detached member JPA实体的集合成员分离或不受管理 - JPA entity's collection members get detached or not managed 为什么 jpa/hibernate 在保存从“findBy”方法检索的实体时仍然执行“select-then-update”,似乎它已分离 - Why does jpa/hibernate still perform a “select-then-update” when saving an entity that retrieved from “findBy” method, seems it was detached Spring JPA - 实体分离? 关于例外? - Spring JPA - entity detached? on exceptions? Spring数据jpa分离实体 - Spring data jpa detached entity JPA独立实体已传递以保留 - JPA detached entity passed to persist 有没有办法将分离的对象传递给JPA持久化? (传递给持久化的分离实体) - Is there a way to pass detached object to JPA persist? (detached entity passed to persist) JPA错误:InvalidDataAccessApiUsageException:分离的实体传递给持久化 - JPA Error: InvalidDataAccessApiUsageException: detached entity passed to persist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM