简体   繁体   English

Hibernate:我们可以使用 Hibernate 删除父对象而不删除子对象吗?

[英]Hibernate : Can we delete parent object without delete the child object using Hibernate?

Hi guys Is it possible to delete the parent object without deleting child object in Hibernate ?嗨,伙计们,是否可以在不删除 Hibernate 中的子对象的情况下删除父对象? In My Case child has multiple parent So I just want to remove one parent object from child object.在我的案例中,孩子有多个父对象所以我只想从子对象中删除一个父对象。

Yes, though you must remember about two things:是的,尽管您必须记住两件事:

1) Your child entity must allow null on the foreign key to the child 1)您的子实体必须允许子实体的外键为 null

2) You must remember to NOT have CascadeType.DELETE on the @OneToMany relationship in the parent. 2)您必须记住不要在父级中的@OneToMany 关系上使用 CascadeType.DELETE。

3) Before removing the parent, clear the children collection first: 3)在移除parent之前,先清除children集合:

parent.setChildren(null);
session.delete(parent);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM