报错内容:
nested exception is org.hibernate.HibernateException:A collection with cascade="all-delete-orphan"was no longer referenced by the owning entity instance: com.**.**.bo.requirementForm.entities.RequirementForm.hardwareList
原因:
级联更新集合字段是一个持久的集合,存在hibernate的对象池里面,如果直接使用默认的set方法就把对象的引用指向hibernate对象池外了,需要修改这个集合而不是重新指定一个新的集合到类中
解决办法:
重写set方法(级联相关的字段都需要注意!!!)
// 字段初始化
@OneToMany(orphanRemoval = true, cascade = { CascadeType.ALL }) @JoinColumn(name = "requirement_id") private List<Workload> workloads = new ArrayList<Workload>();
// set方法需要重写
public void setWorkloads(List<Workload> workloads) { if(workloads == null || workloads.size() == 0){ this.workloads = new ArrayList<>(); }else if(this.workloads == null || workloads.size() != 0){ this.workloads = new ArrayList<>(); this.workloads.addAll(workloads); }else{ this.workloads.clear(); this.workloads.addAll(workloads); } }
标签:set,JPA,orphan,级联,报错,hibernate,workloads From: https://www.cnblogs.com/l-926/p/17407640.html