a different object with the same identifier value was already associated with the session。
来源:互联网 发布:淘宝卖家怎么催快递 编辑:程序博客网 时间:2024/06/10 07:20
原因是因为在hibernate中同一个session里面有了两个相同标识
但是是不同实体,当这时运行saveOrUpdate(object)操作的时候就会报这个错误。
既然是因为不同实体而产生的错误,那么我就只创建一个实体类:
一开始我是这样写的:
然后后来我就改成只有一个实体类错误就消失了:
会报出"Found two representations of same collection",
最后通过session.refresh(object)方法就可以解决了,注意,当object不
是数据库中已有数据的对象的时候,不能使用session.refresh(object)因
为refresh是从hibernate的session中去重新取object,如果session中没
有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判
断一下。当然这个问题最容易解决的办法还是使用Hibernate里面自带的merge()
方法。这种错误还经常出现在一对多映射和多对多映射。
1、a different object with the same identifier value was already associated with the session。
错误原因:在hibernate中同一个session里面有了两个相同标识但是是不同实体。
解决方法一:session.clean()
PS:如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出"Found two representations of same collection"异常。
解决方法二:session.refresh(object)
PS:当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。
解决方法三:session.merge(object)
PS:Hibernate里面自带的方法,推荐使用。
2、Found two representations of same collection
错误原因:见1。
解决方法:session.merge(object)
以上两中异常经常出现在一对多映射和多对多映射中
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session错误
- a different object with the same identifier value was already associated with the session的解决办法
- 解决a different object with the same identifier value was already associated with the session错误
- a different object with the same identifier value was already associated with the session
- a different object with the same identifier value was already associated with the session
- 解决a different object with the same identifier value was already associated with the session错误
- a different object with the same identifier value was already associated with the session
- a different object with the same identifier value was already associated with the session
- 再次碰到:a different object with the same identifier value was already associated with the session
- a different object with the same identifier value was already associated with the session
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session错误
- HibernateSystemException: a different object with the same identifier value was already associated with the session.
- a different object with the same identifier value was already associated with the session
- a different object with the same identifier value was already associated with the session
- 解决a different object with the same identifier value was already associated with the session
- 解决a different object with the same identifier value was already associated with the session错误
- Ajax总结
- KMP算法求模式串在原串中出现的次数
- linux下ssh服务器安装与登录
- 新的工作,新的开始
- 进程和线程区别
- a different object with the same identifier value was already associated with the session。
- nvm在window下的配置
- jquery实现网页换肤
- 文章标题
- java面向对象的封装、继承、多态案例
- 从JAVA到Scala(三):implicit的三种用法
- 洛谷 1414 又是毕业季II 数论
- hdu 2063 二分图匹配入门题 匈牙利算法
- 第一章