[英]JPA: Delete children in OneToMany
我有一个OneToMany映射,如下所示
家长:
@OneToMany(mappedBy = parent, orphanRemoval = true, cascade = { CascadeType.ALL})
private List<Child> childs = new ArrayList<Child>();
儿童:
@ManyToOne
@JoinColumn(name = "parentid")
private Parent parent;
在更新父实体时,我得到一个例外:
A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance
我的代码:
parent.getChild().clear();
parent.setChild(childList)
您可以从父级中删除子级,但是由于“ Child
是拥有方,因此您必须从“ Child
实体中删除对“ Parent
的引用。
for (Child child: children) {
child.setParent(null);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.