gpt4 book ai didi

java - Hibernate saveOrUpdate 方法会删除 child 吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:18:37 24 4
gpt4 key购买 nike

我有一个应用程序通过 hibernate 加载对象,然后将这些对象作为分离对象传递到另一层。对这些对象所做的任何更改都会向下发送回 hibernate 层,在 hibernate 层中我会在这些对象上调用 saveOrUpdate()

如果我在调用 之前简单地从集合中删除子对象,hibernate 是否会删除传递给 saveOrUpdate() 的对象中包含在集合中的一对多关系子对象保存或更新()

如果不是,那么这通常如何在使用分离对象的 hibernate 应用程序中完成?

最佳答案

Will hibernate delete one-to-many relationship child objects contained in a collection in the objects that are passed into saveOrUpdate() if I simply remove the child object from the collection before calling saveOrUpdate()?

不,不是默认情况。在这种情况下,此类子对象被称为“孤儿”,假设其他实体也没有对它们的引用。

这在文档中进行了讨论,11.11. Transitive persistence :

A special cascade style, delete-orphan, applies only to one-to-many associations, and indicates that the delete() operation should be applied to any child object that is removed from the association. Using annotations there is no CascadeType.DELETE-ORPHAN equivalent. Instead you can use the attribute orphanRemoval as seen in Example 11.4, “@OneToMany with orphanRemoval”. If an entity is removed from a @OneToMany collection or an associated entity is dereferenced from a @OneToOne association, this associated entity can be marked for deletion if orphanRemoval is set to true.

关于java - Hibernate saveOrUpdate 方法会删除 child 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10506056/

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