做项目的时候遇到一个很头疼的报错: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. 老李分享:JVM调优

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

  2. Java并发编程:volatile 关键字

    转自:http://www.cnblogs.com/aigongsi/archive/2012/04/01/2429166.html 其实Java语言是支持多线程的,为了解决线程并发的问题,在语言内部 ...

  3. MongoDB基础教程系列--第七篇 MongoDB 聚合管道

    在讲解聚合管道(Aggregation Pipeline)之前,我们先介绍一下 MongoDB 的聚合功能,聚合操作主要用于对数据的批量处理,往往将记录按条件分组以后,然后再进行一系列操作,例如,求最 ...

  4. Python os与sys模块解析

    os与sys模块的官方解释如下: os: This module provides a portable way of using operating system dependent functio ...

  5. (读书笔记)函数参数浅析-JavaScript高级程序设计(第3版)

    ECMAScript函数不介意传递的参数个数,因为在其内部是用一个数组进行表示的.在函数体内可以通过arguments对象来访问这个参数数组,就像我们正常访问数组一样处理. arguments对象只是 ...

  6. 关于阿里图标库Iconfont生成图标的三种使用方式(fontclass/unicode/symbol)

    1.附阿里图标库链接:http://www.iconfont.cn/ 2.登录阿里图标库以后,搜索我们需要的图标,将其加入购物车,如图3.将我们需要的图标全部挑选完毕以后,点击购物车图标4.这时候右侧 ...

  7. 使用纯CSS方案,解决垂直居中

    CSS是HTML元素的剪刀手,它极度的丰富了web页面的修饰.在众多CSS常见的样式需求中,有一奇葩式的存在[垂直居中],因为不管是从逻辑实现方面还是从正常需求量来讲,这都没理由让这个需求在实践过程中 ...

  8. Java多线程学习笔记(二)——Executor,Executors,ExecutorService比较

    Executor:是Java线程池的超级接口:提供一个execute(Runnable command)方法;我们一般用它的继承接口ExecutorService. Executors:是java.u ...

  9. 使用Perl提取Excel中的IO_MUX

    使用Perl提取Excel中的IO_MUX 关键问题 提取数据 格式化输出 循环嵌套 数据结构构建 坐标映射,逆向提取关键字 描述 在IC集成中,我们使用Excel表格规划设计的IC引脚功能映射需要转 ...

  10. 【算法系列学习】线段树 单点覆盖,区间查询最大值 [kuangbin带你飞]专题七 线段树 B - I Hate It

    https://vjudge.net/contest/66989#overview #include<iostream> #include<cstdio> #include&l ...