A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.ifreecomm.education.business.domain.model.questionIdentify.TaskIdentifyPage.partInfos; nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance: com.ifreecomm.education.business.domain.model.questionIdentify.TaskIdentifyPage.partInfos
JPA 更新数据库异常:A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance。
该异常是因为级联更新关联表数据时操作错误导致的
@Setter
@OneToMany(orphanRemoval = true, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "identifyPageId")
private Set<TaskIdentifyPartInfo> partInfos = new HashSet<>();
更新该字段时不能使用setPartInfos(),需要使用set类的add或者addAll方法进行更新。
正确操作为:
public void updatePartInfos(Set<TaskIdentifyPartInfo> partInfos) {
if (CollectionUtils.isEmpty(partInfos)) {
return;
}
this.partInfos.clear();
this.partInfos.addAll(partInfos);
}