Hibernate中遇到的一个Exception

做项目的时候遇到一个很头疼的报错:Illegal attempt to associate a collection with two opensessions 

出错原因:  一个Object Version 包含了两个oneTomany关系。在数据库调取出一个version对象时,再用saveOrUpdate这个对象就会出现如上错误

解决办法:  讲session.saveOrUpdate(version)改为session.merge(version)

原理分析:  saveOrUpdate是在同一个session里用于保存或更新一个条目,而merge可以用于两个不同session数据的保存或更新。
HibernateReference里说:

saveOrUpdate() does the following:

  • if the object is already persistent in this session, donothing
  • if another object associated with the session has the sameidentifier, throw an exception
  • if the object has no identifier property, save() it
  • if the object's identifier has the value assigned to a newlyinstantiated object, save() it
  • if the object is versioned by a or , and the version property valueis the same value assigned to a newly instantiated object, save()it
  • otherwise update() the object

merge() is very different:

  • if there is a persistent instance with the same identifiercurrently associated with the session, copy the state of the givenobject onto the persistent instance
  • if there is no persistent instance currently associated with thesession, try to load it from the database, or create a newpersistent instance
  • the persistent instance is returned
  • the given instance does not become associated with the session, itremains detached

网上有其他资料说代码如下:entity=merge(entity);  saveOrUpdate(entity);这是错误的,第二行代码完全没有必要,因为entity已经是persistentobject,会被saveOrUpdate忽略。直接用session.merge(version)即可。

posted @   小张的练习室  阅读(322)  评论(0)    收藏  举报
编辑推荐:
· 复杂业务系统线上问题排查过程
· 通过抓包,深入揭秘MCP协议底层通信
· 记一次.NET MAUI项目中绑定Android库实现硬件控制的开发经历
· 糊涂啊!这个需求居然没想到用时间轮来解决
· 浅谈为什么我讨厌分布式事务
阅读排行:
· Coze Studio:字节跳动 Coze 的开源版本来了!第一时间深度解析
· 为大模型 MCP Code Interpreter 而生:C# Runner 开源发布
· 复杂业务系统线上问题排查过程
· 独立开发:高效集成大模型,看这篇就够了
· 在SqlSugar的开发框架的Vue3+ElementPlus前端中增加对报表模块的封装处理,实现常
点击右上角即可分享
微信分享提示