在进行jpa更新操作中报异常Cascade="All-Delete-Orphan" 处理

来源:互联网 发布:人工智能a7论坛 编辑:程序博客网 时间:2024/06/05 10:21

有两个实体一个是exam,一个是question,关联关系是@OneToMany

今天在做一个更新的操作总遇到一个异常:A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance


刚开始是这么写的。

exam.setQuestions(questionList);

但是会报上面的异常,

后来发现exam的list<question>是一个持久的集合存在hibernate的对象池里面,如果直接exam.setQuestions(questionList);就把exam对question对象的引用指向hibernate对象池外了。

后来经过网上查询资料做法是:

exam.getQuestions().clear();
exam.getQuestions().addAll(questionList);

这样就可以解决上述异常,测试的多次没有发现问题。特此记录下。

参考文章:http://www.verydemo.com/demo_c146_i7297.html

0 0
原创粉丝点击