我有一个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)
答案 0 :(得分:1)
您从父级移除子级,但由于Child
是拥有者,您必须从Parent
实体中删除对Child
的引用。
for (Child child: children) {
child.setParent(null);
}