EntityManager remove()是否引起分离 [英] Does EntityManager remove() cause detachment

查看:129
本文介绍了EntityManager remove()是否引起分离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否为事务范围内的实体管理器以外的对象调用EntityManager.remove(someEntity)导致实体分离?在我阅读JSR-317时:

Does calling EntityManager.remove(someEntity) -for other than transaction scoped entity manager- cause the entity to be detached? As I was reading the JSR-317 :

如果 使用事务范围的容器管理的实体管理器(请参阅 第3.3节);从事务回滚(请参阅第3.3.2节);从 将实体与持久性上下文分离;从清除 持久性环境;从关闭实体经理开始;或来自 序列化实体或通过值传递实体,例如 单独的应用程序层,通过远程接口等.

A detached entity results from transaction commit if a transaction-scoped container-managed entity manager is used (see section 3.3); from transaction rollback (see section 3.3.2); from detaching the entity from the persistence context; from clearing the persistence context; from closing an entity manager; or from serializing an entity or otherwise passing an entity by value—e.g., to a separate application tier, through a remote interface, etc.

它没有提到remove()导致实体分离,尽管其他网站也提到了它.请提供您答案的参考.

It didn't mention that remove() causes the entity to be detached, though other web sites mentions it. please provide a reference to your answer.

推荐答案

根据

According to the JPA 2.1 specification (Section 3.2.3 Removal, PDF page 82), we find an important piece of information, related to your question:

实体删除后,其状态(生成状态除外)将是该实体在该点处的 remove操作被称为.

After an entity has been removed, its state (except for generated state) will be that of the entity at the point at which the remove operation was called.

因此,答案似乎是: 这取决于.

So, the answer seems to be: It depends.

但是,在第80页上,我们发现了另一重要信息.

Yet, on page 80, we find another important bit of information.

已删除实体实例是具有持久身份的实例,该实例与持久性上下文相关联,将在提交事务后从数据库中将其删除.

A removed entity instance is an instance with a persistent identity, associated with a persistence context, that will be removed from the database upon transaction commit.

因此,我们可以推断出它尚未处于 detached 状态,因为它仍然是关联的",直到最终被TX提交删除为止.

Thus, we can infer that it is not in a detached state as it is still "associated" until finally removed by the TX commit.

深入研究,您可以参考 JPA生命周期映像. /stackoverflow.com/users/1025118/vlad-mihalcea>弗拉德·米哈尔西娅(Vlad Mihalcea)来"更好地理解JPA状态转换".如第一张图片(JPA)所示,实体不会过渡到 detached 状态,但最终会处于 removed 状态.

Digging deeper, you can consult the JPA lifecycle image provided in the answer by Vlad Mihalcea to "understand the JPA state transitions better". As depicted in the first image (JPA), an entity does not transit to the detached state, but ends up in a removed state.

有关remove(..)操作语义的更多详细信息,请参见JPA Spec文档的第81/82页.

Further details on the semantics of the remove(..) operation are found on page 81/82 of the JPA Spec document.

希望有帮助.

这篇关于EntityManager remove()是否引起分离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆