a different object with the same identifier value was already (用hibernate的update方法一次请求多次update时出现的错)
来源:互联网 发布:沧海一声笑歌词知乎 编辑:程序博客网 时间:2024/05/22 16:00
用Hibernate的update时,多次执行回报这个错
a different object with the same identifier value was already
associated with the session:
因为同一个session里面有了两个相同标识但是是不同实体
update方法:
/** * @param object * @return * @throws HibernateException */public boolean saveOrUpdate(Object object) {System.out.println("=============== saveOrUpdate() start =================");Session session = null;Transaction tx = null;logger.info("saveOrUpdate(Object) - start");try {session = SessionFactory.getSession();tx = session.beginTransaction();//session.refresh(object);//session.clear();session.saveOrUpdate(object);tx.commit();SessionFactory.closeSession();logger.info("saveOrUpdate(Object) - end");System.out.println("============== saveOrUpdate() end =================");return true;} catch (HibernateException e) {logger.error("saveOrUpdate(Object object) throw exception = ", e);try {if (tx != null)tx.rollback();} catch (HibernateException ex) {throw new RuntimeException("tx.rollbacd() throw exception = "+ ex.toString());}throw new RuntimeException("saveOrUpdate(Object) throw exception = " + e.toString());} finally {try {if (session != null)SessionFactory.closeSession();} catch (HibernateException e) {throw new RuntimeException("SessionFactory.closeSession() throw exception = "+ e.toString());}}}
后来在网上找到了3种办法:
(解决方法一: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里面自带的方法,推荐使用。
)
自己都试了,发现我用session.merge(object) 一定报同样的错误;
session.refresh(object) 偶尔报同样的错误;
Found two representations of same collection这个错误没遇到。
只有用session.clean()的时候正常!
- a different object with the same identifier value was already (用hibernate的update方法一次请求多次update时出现的错)
- hibernate在saveOrUpdate时,update报错:a different object with the same identifier value was already assoc
- Hibernate调用update()的时候出现a different object with the same identifier value...异常
- a different object with the same identifier value was already associated with the session 一个经典的hibernate错误
- 开发中遇到的hibernate异常:a different object with the same identifier value was already associated with the
- 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的解决
- 托管态update更新异常——a different object with the same identifier value was already associated withthe sess
- Hibernate Exception: 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 session
- [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
- 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 session
- 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 session
- hibernate报错a different object with the same identifier value was already associated with the session
- 定义一个CMS的前景如何?
- C++上机报告 利用公式计算π=4(1-1/3+1/5-1/7+1/9-...)的近似值,直到括号中最后一项的绝对值小于0.000001为止。
- make 变量 赋值 区别
- 七夕情书之恋恋红尘
- WORD上次启动时失败,以安全模式启动WORD将帮助您纠正或发现启动中的问题
- a different object with the same identifier value was already (用hibernate的update方法一次请求多次update时出现的错)
- multimap使用lower_bound、 upper_bound、 equal_range进行特定key的遍历
- 泛型
- Schema约束XML文件的实例
- if no GUI program,you can use named Event to communicate
- Apache 虚拟主机 VirtualHost 配置
- 关于Android Force Close 出现的原因 以及解决方法
- 我的华为HG255D开机显示信息
- 家乡的小河