JPA:删除OneToMany中的孩子

时间:2014-04-24 08:40:17

标签: java jpa jpa-2.0 children

我有一个OneToMany映射,如下所示

父:

@OneToMany(mappedBy = parent, orphanRemoval = true, cascade = { CascadeType.ALL})
private List<Child> childs = new ArrayList<Child>();

子:

@ManyToOne
@JoinColumn(name = "parentid")
private Parent parent;

在更新Parent实体时,我得到一个例外:

A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance

我的代码:

parent.getChild().clear();
parent.setChild(childList)

1 个答案:

答案 0 :(得分:1)

您从父级移除子级,但由于Child是拥有者,您必须从Parent实体中删除对Child的引用。

for (Child child: children) {
   child.setParent(null);
}