[Java]Hibernate保存或者更新VO遇到same identifier value问题
来源:互联网 发布:js中单双引号的区别 编辑:程序博客网 时间:2024/06/05 14:41
最近在使用到了hibernate更新一个VO遇到了这个问题
a different object with the same identifier value was already associated with the session
当
session.update(vo);
就提示上面的错误。是Nhibernate里头,在同一个Session里,数据库的一条记录,只能对应一个引用。如果程序中有其他的对象(这个对象不是由这个Session 从数据库取得的,叫脱管实体,和Session中某一对象的数据库标识符相同)由这个Session保存。就会报这个错,目的在于保证不出现不一致读的问题。
仔细想了一下也是,因为数据是从Flex客户端传过去的,我是修改用户数据,之前用这个用户数据登陆了。
知道了问题所在,只要清楚掉session的一些数据就没问题了
session.clear();
使用了这个方法后再更新数据,成功解决问题。
后来再网上搜索一下,发现很多人也提出了问题。下面是一个人的总结
http://blog.csdn.net/piggerman/archive/2008/08/21/2804556.aspx
最后他所的几种方法:
Hibernate 疑难异常及处理
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
当
session.update(vo);
就提示上面的错误。是Nhibernate里头,在同一个Session里,数据库的一条记录,只能对应一个引用。如果程序中有其他的对象(这个对象不是由这个Session 从数据库取得的,叫脱管实体,和Session中某一对象的数据库标识符相同)由这个Session保存。就会报这个错,目的在于保证不出现不一致读的问题。
仔细想了一下也是,因为数据是从Flex客户端传过去的,我是修改用户数据,之前用这个用户数据登陆了。
知道了问题所在,只要清楚掉session的一些数据就没问题了
session.clear();
使用了这个方法后再更新数据,成功解决问题。
后来再网上搜索一下,发现很多人也提出了问题。下面是一个人的总结
http://blog.csdn.net/piggerman/archive/2008/08/21/2804556.aspx
最后他所的几种方法:
Hibernate 疑难异常及处理
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)
以上两中异常经常出现在一对多映射和多对多映射中
来自:http://www.sujun.org/article.asp?id=43
- [Java]Hibernate保存或者更新VO遇到same identifier value问题
- Hibernate更新a different object with the same identifier value was already associated with the session
- hibernate保存报错:a different object with the same identifier value was already associated with the ses
- 开发中遇到的hibernate异常:a different object with the same identifier value was already associated with the
- Hibernate Exception: a different object with the same identifier value was already associated with the session
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread
- org.hibernate.NonUniqueObjectException:a different object with the same identifier value was alread
- [Hibernate]a different object with the same identifier value was already associated with the session
- Hibernate异常: a different object with the same identifier value was already associated with the sessi
- Hibernate错误:a different object with the same identifier value was already associated with the sessio
- org.hibernate.NonUniqueObjectException:a different object with the same identifier value was alread
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was ...
- Hibernate:a different object with the same identifier value was already associated with the session
- org.hibernate.NonUniqueObjectException: A different object with the same identifier value was alread
- org.hibernate.NonUniqueObjectException: A different object with the same identifier value was alread
- hibernate异常:a different object with the same identifier value was already associated
- Qualcomm Android display架构分析
- 第四步:Menu事件、常用布局、RadioButton和CheckBox使用方法、Toast的使用方法
- 一些软件设计的原则
- 怎么进行SEO网站优化
- android分割线渐变色和几种颜色值定义
- [Java]Hibernate保存或者更新VO遇到same identifier value问题
- arm学习笔记之Arm异常
- JavaScript 跑马灯源码
- jdbc批量插入、批量删除、批量更新
- failed to lazily initialize a collection of role
- 9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
- 割接工作总结
- The Link List with C
- 榆花荚科技开发有限公司工程师徐龙捷的访谈