您的位置:首页 > Web前端

hibernate异常"Found shared references to a collection 解决方法

2013-10-17 19:25 441 查看
假定,Parent类有一个Set属性,里面放的是Son。如果查询"from Parent",某个Parent哪怕一个Son都没有,那个Set属性不会为null,而是一个空集合。

这时候如果你Parent newP=new Parent();然后BeanUtils.copyPropertis(newP,origP);最后就会报hibernate异常"Found shared references to a collection"。

注:hibernate在什么时机发现"两个对象共享一个集合"的情况的?我这边的例子是在下一次查询时发现的。

原因:

BeanUtils.copyPropertis是浅拷贝,导致这两个对象引用的Set是同一个Set,这在hibernate中是不允许的,参见Hibernate reference第6章的"Two entities may not share a reference to the same collection instance"。

这种问题常见于复制对象时。

如何解决:newP.setSonSet(null);

还有人说原因可能是并发操作:http://www.blogjava.net/fastzch/archive/2006/12/22/89520.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: