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

标签 java hibernate

我有一个应用程序通过 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/

相关文章:

java - 使用 slf4j 作为 log4j2 的抽象

java - 使用 for 循环查找数组中缺失的值

java - RSA 加密 :InvalidKeyException: invalid key format

java - persistence.xml 文件的属性的含义是什么

java - 如何获取通过 Hibernate 连接的数据库名称?

java - Hibernate不创建表

java - Spring Neo4j无法添加关系

java - 使用内连接将多个 sql 查询转为单个查询

oracle - 没有更多数据可从套接字错误中读取

java - Hibernate - Envers - 多个审计目标