一、分析错误的原因
首先先理解下“Found shared references to a collection”这句话的意思,发现共享了同一个集合应用
意思是说禁止两个对象共同引用同一个集合对象,参见Hibernate reference第6章的”Two entities may not share a reference to the same collection instance”。
举个例子
当parent里有son的集合时,查询出来的parent里的son集合是非空对象。即时该parent没有son。
在经过BeanUtil.copyPropertis(newObject,oldObject)时,由于是一个浅拷贝(意思是只拷贝对象本身和基本变量,不拷贝对象中引用的对象)。
为了更好的说明浅拷贝,用上面的实例举例
假设ParentA里有sonA,经过BeanUTtil(ParentB,ParentA)。该ParentB对象中的son集合应用还是sonA。
二、解决办法
- 将拷贝后的对象中集合引用set为null
- 用深拷贝
参考
浅拷贝和深拷贝
https://www.cnblogs.com/xuanxufeng/p/6558330.html