hibernate的orphanRemoval

时间:2023-03-09 16:36:09
hibernate的orphanRemoval

在@OneToMany与@OneToOne中使用orphanRemoval = true时候

改动保存时候setXXX

org.springframework.orm.hibernate3.HibernateSystemException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:

解决方式:

1.将setXXX改为private

2.加入一个add方法

public void addXXX(List<T> ts)

{

  this.ts.clear();

  if (null != ts&& !ts.isEmpty())

     {

       this.ts.addAll(ts);

     }

}

须要setXXX时使用addXXX