简体   繁体   English

对分离实体的“删除”操作

[英]“Remove” operation on detached entities

I have a question about the remove operation on detached entities. 我对分离实体上的删除操作有疑问。

According to the Spec "If X is a detached entity, an IllegalArgumentException will be thrown by the remove operation (or the transaction commit will fail)." 根据规范“如果X是一个分离的实体,则remove操作将引发IllegalArgumentException(否则事务提交将失败)。”

But the remove operation can be valid for a detached entity. 但是删除操作对于分离的实体可能有效。 Consider the following scenario for a transaction-scoped persistence context: 对于事务范围的持久性上下文,请考虑以下情形:

1) An entity is persisted in the database in transaction T1. 1)实体在事务T1中保留在数据库中。 So it becomes detached. 因此它变得分离。 2) A remove call is made for this detached entity in transaction T2. 2)在事务T2中对此分离实体进行了删除调用。

Because the entity exists in the database, the remove call should be valid and the entity should be removed from the database as a result of transaction T2. 因为实体存在于数据库中,所以删除调用应该是有效的,并且由于事务T2,应该从数据库中删除该实体。 But according to the spec, an exception should be thrown by remove or transaction commit should fail. 但是根据规范,应该由remove引发异常,否则事务提交将失败。

Am I missing something here? 我在这里想念什么吗? what is the reason? 是什么原因?

detached entities are a non managed entities , and you are trying to deleting the non managed entity,If you want to remove it, then you have to make it in a managed state . detached entities non managed entities ,您要删除该非托管实体。如果要删除它,则必须使其处于managed state or one another way is to find out the references of detached entity and try to remove. 或者另一种方法是找出分离的实体的引用并尝试删除。

getEntityManager().getReference("detached");
remove("reference");

I am not sure about the above line, but you may try it. 我不确定上述内容,但您可以尝试一下。

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

相关问题 休眠:commit()之后脱离实体 - Hibernate: detached of entities after commit() 为什么需要JPA中的分离实体? - Why need detached entities in JPA? Eclipselink - 分离实体内存泄漏 - Eclipselink - detached entities memory leak 删除实体时,“ App Engine JPA 2获取错误消息”“尝试删除分离的实例,但操作要求将其附加” - App Engine JPA 2 get error message when deleting an entity “trying to remove detached instance but operation requires it to be attached” 外部事务中获取的实体是托管还是分离? - Are entities fetched outside transaction managed or detached? Hibernate 如何区分临时实体和分离实体? - How Hibernate differs transient and detached entities? 具有扩展持久化上下文的意外分离实体 - Unexpected detached entities with extended persistent context 合并独立的JPA实体,忽略未修改的字段 - Merging Detached JPA Entities Ignoring Unmodified Fields 了解:“无法对分离的对象执行操作删除” - Understanding: “Cannot perform operation delete on detached object” JPA级联持久化并且对分离实体的引用会抛出PersistentObjectException。为什么? - JPA cascade persist and references to detached entities throws PersistentObjectException. Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM