gpt4 book ai didi

java - @ManyToOne JPA 关联和级联...不确定如果我删除一个对象会发生什么

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:04:22 25 4
gpt4 key购买 nike

我对级联在删除操作中的工作方式还不是很清楚。我想知道如果我有这个会发生什么:

class myBean{

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
public Cliente getClienteDiAppartenenza() {
return clienteDiAppartenenza;
}

}


class Cliente{
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
public List<myBean> getMyBeans() {
return myBeans;
}
}

如果我删除具有此属性的 myBean,我不确定链接的 Cliente 是否也会被删除(在 manytoone 中很奇怪)或者 Cliente 中的集合将被更新并且 myBean 的那个实例被删除,然后被保存。

会发生什么?? Hibernato 文档对此不是很清楚...

最佳答案

这不是 Hibernate 的东西,这是 JPA 2.0 标准的一部分。您的注释有两个方面,一件事是使用 orphanRemoval。

当父实体控制子实体的创建和销毁时,您可以使用 orphanRemoval。在 UML 中,这将是一个组合的情况,它是整体对部分的强大所有权和可靠的生命周期。 2.9 节中的 JPA 2.0 规范:实体关系说:

Associations that are specified as OneToOne or OneToMany support use of the orphanRemoval option. The following behaviors apply when orphanRemoval is in effect:

  1. If an entity that is the target of the relationship is removed from the relationship (by setting the relationship to null or removing the entity from the relationship collection), the remove operation will be applied to the entity being orphaned. The remove operation is applied at the time of the flush operation. The orphanRemoval functionality is intended for entities that are privately "owned" by their parent entity. Portable applications must otherwise not depend upon a specific order of removal, and must not reassign an entity that has been orphaned to another relationship or otherwise attempt to persist it. If the entity being orphaned is a detached, new, or removed entity, the semantics of orphanRemoval do not apply.

  2. If the remove operation is applied to a managed source entity, the remove operation will be cascaded to the relationship target in accordance with the rules of section 3.2.3, (and hence it is not necessary to specify cascade=REMOVE for the relationship)[20].

第二个方面是在没有隐含 orphanRemoval 时使用 cascase=REMOVE。

第 3.2.3 节:删除包含有关删除过程的详细信息:

The semantics of the remove operation, applied to an entity X are as follows:

• If X is a new entity, it is ignored by the remove operation. However, the remove operation is cascaded to entities referenced by X, if the relationship from X to these other entities is annotated with the cascade=REMOVE or cascade=ALL annotation element value.

• If X is a managed entity, the remove operation causes it to become removed. The remove operation is cascaded to entities referenced by X, if the relationships from X to these other entities is annotated with the cascade=REMOVE or cascade=ALL annotation element value.

• If X is a detached entity, an IllegalArgumentException will be thrown by the remove operation (or the transaction commit will fail).

• If X is a removed entity, it is ignored by the remove operation.

A removed entity X will be removed from the database at or before transaction commit or as a result of the flush operation. 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.

关于java - @ManyToOne JPA 关联和级联...不确定如果我删除一个对象会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6189928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com