我有一个应用程序通过 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 callingsaveOrUpdate()
?
不,不是默认情况。在这种情况下,此类子对象被称为“孤儿”,假设其他实体也没有对它们的引用。
这在文档中进行了讨论,11.11. Transitive persistence :
A special cascade style,
delete-orphan
, applies only to one-to-many associations, and indicates that thedelete()
operation should be applied to any child object that is removed from the association. Using annotations there is noCascadeType.DELETE-ORPHAN
equivalent. Instead you can use the attribute orphanRemoval as seen in Example 11.4, “@OneToMany
withorphanRemoval
”. 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 iforphanRemoval
is set to true.
关于java - Hibernate saveOrUpdate 方法会删除 child 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10506056/