a different object with the same identifier value was already associated with the session 深入分析
来源:互联网 发布:java 内存泄漏 例子 编辑:程序博客网 时间:2024/05/21 21:44
一、先贴上网络上摘的文章
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里面自带的方法,推荐使用。
二、 我的分析
背景知识
DocType 里面有一个WebSite对象属性
错误代码
/** * 删除单条记录 * * @param id 用于删除的记录的id * @return 成功删除的记录数 */ public void delete(Website website) throws Exception { DocType docType = getDocTypeBS().getDocTypeByCode(ROOT, website.getId()); if (docType != null) { Set<DocType> docTypeSet = docType.getChildren(); if (docTypeSet != null && docTypeSet.size() > 0) { throw new Exception("此站点下有子栏目,不能删除!"); } else { getDocTypeBS().delete(docType); getDao().delete(website); } } }
修正代码
/** * 删除单条记录 * * @param id 用于删除的记录的id * @return 成功删除的记录数 */ public void delete(Website website) throws Exception { DocType docType = getDocTypeBS().getDocTypeByCode(ROOT, website.getId()); if (docType != null) { Set<DocType> docTypeSet = docType.getChildren(); if (docTypeSet != null && docTypeSet.size() > 0) { throw new Exception("此站点下有子栏目,不能删除!"); } else { //此处要注意,删除时不能传入参数的website对象,因为上面查出来的docType里面级联了此site对象,再传入参数时会在session缓存中会出现两个主键相同的实体 getDao().delete(docType.getSite()); getDocTypeBS().delete(docType); } } }
我的分析
这是由于查询DocType的时候级联查出了Website,导致Session中已经有了此Website实例了,再把游离态的webSite对象传入就会导致二个ID相同的对象,Hibernate会报错的.
- 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错误
- 解决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
- html---备忘基础
- poj1007玛雅历
- linux5------vi编辑器的使用、命令
- 用tbody代替div 解决 table tr的隐藏问题
- Java正则表达式教程
- a different object with the same identifier value was already associated with the session 深入分析
- root 运行 Google Chrome
- div被选择框select遮挡住的5种解决方法
- SharePoint网站搜索不到任何内容
- Web——HTML5特性检测
- QQ在线客服代码 飘浮在右侧的QQ客服js特效演示
- 如何在asp.net使用Javascript
- 程序员总结:帮助你早些明白一些道理
- win 2008 r2 + iis7,ubuntu server 服务器设置入门