做项目的时候遇到一个很头疼的报错: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)即可。

Hibernate中遇到的一个Exception的更多相关文章

  1. HttpSession与Hibernate中Session的区别

    一.javax.servlet.http.HttpSession是一个抽象接口 它的产生:J2EE的Web程序在运行的时候,会给每一个新的访问者建立一个HttpSession,这个Session是用户 ...

  2. hibernate 中createQuery与createSQLQuery两个用法

    hibernate 中createQuery与createSQLQuery两者区别是:前者用的hql语句进行查询,后者可以用sql语句查询前者以hibernate生成的Bean为对象装入list返回后 ...

  3. Hibernate中evict方法和clear方法说明

    Hibernate中evict方法和clear方法说明 先创建一个对象,然后调用session.save方法,然后调用evict方法把该对象清除出缓存,最后提交事务.结果报错: Exception i ...

  4. hibernate中@Entity和@Table的区别

    Java Persistence API定义了一种定义,可以将常规的普通Java对象(有时被称作POJO)映射到数据库.这些普通Java对象被称作Entity Bean.除了是用Java Persis ...

  5. [原创]java WEB学习笔记81:Hibernate学习之路--- 对象关系映射文件(.hbm.xml):hibernate-mapping 节点,class节点,id节点(主键生成策略),property节点,在hibernate 中 java类型 与sql类型之间的对应关系,Java 时间和日期类型的映射,Java 大对象类型 的 映射 (了解),映射组成关系

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. Hibernate中Session的get和load

    hibernate中Session接口提供的get()和load()方法都是用来获取一个实体对象,在使用方式和查询性能上有一些区别.测试版本:hibernate 4.2.0. get Session接 ...

  7. Hibernate中的PO

    Hibernate中的PO PO就是持久化对象,它的作用就是完成持久化操作,即通过以面向对象的方式操作该对象对数据库中的数据执行增.删.改.查的操作. Hibernate是低侵入式的设计,完全采用Ja ...

  8. hibernate中session的获取使用以及其他注意事项

    hibernate中session的获取使用以及其他注意事项 前言:工作时,在同时使用Hibernate的getSession().getHibernateTemplate()获取Session后进行 ...

  9. 关于Hibernate中的Configuration

    Hibernate中,关于从 Configuration中建立一个SessionFactory常用的可以有两种方法,一种是为Configuration提供hibernate.cfg.xml配置文件,还 ...

随机推荐

  1. 老李分享: JSON

    老李分享: JSON    poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478, ...

  2. EDP转换IC NCS8803:HDMI转EDP芯片

    HDMI-to-eDP Converter w/ scaler1 Features    Embedded-DisplayPort (eDP) Output    1/2/4-lane eDP @ 1 ...

  3. 在程序加载过程中显示ProgressDialog 对话框

    private ProgressDialog mProgressDlg = null; @Override protected void onCreate(Bundle savedInstanceSt ...

  4. MyBetis3.2框架技术

    1.1    MyBatis介绍 MyBatis 世界上流行最广泛的基于SQ语句的ORM框架,由Clinton Begin 在2002 年创建,其后,捐献给了Apache基金会,成立了iBatis 项 ...

  5. Mysql清理二进制日志的技巧

    1:二进制日志 二进制日志记录了所有的DDL(数据定义语言)语句和DML(数据操作语言)语句,但是不记录包括数据查询的语句.语句以"事件"的形式保存,它描述了数据的更改过程,此日志 ...

  6. Ubuntu16.04部署python2和python3共存的Jupyter Notebook

    一.安装python和python-pip sudo apt-get install python python3 python-pip python3-pip sudo pip install -- ...

  7. Unity3D 预备知识:C#与Lua相互调用

    在使用Unity开发游戏以支持热更新的方案中,使用ULua是比较成熟的一种方案.那么,在使用ULua之前,我们必须先搞清楚,C#与Lua是怎样交互的了? 简单地说,c#调用lua, 是c# 通过Pin ...

  8. Spring aop切面插入事物回滚

    <!-- tx标签配置 事物--> <tx:advice id="txadvice" transaction-manager="transactionM ...

  9. ionic打包项目,运行时报错A problem occurred configuring root project 'android'。。。

    运行报错的原因是sdk没有下载完整 解决办法: 1,打开sdk manage.分别下载android support repository.Google play services.google re ...

  10. Go - 第一个 go 程序 -- helloworld

    创建程序目录 接着上一节的内容,在我们的workspace (D:\Gopher) 里面创建子目录 hello,他的绝对路径为:D:\Gopher\src\github.com\tuo\hello 创 ...