zoukankan      html  css  js  c++  java
  • 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)即可。

  • 相关阅读:
    shell中$0,$?,$!等的特殊用法【转载】
    Hadoop操作hdfs的命令【转载】
    libevent的简单应用【转载】
    使用GDB生成coredump文件【转载】
    常用的js代码
    强大的promise
    循环的N种写法
    原型与继承与class
    模块化
    正则RegExp
  • 原文地址:https://www.cnblogs.com/codingforum/p/4316865.html
Copyright © 2011-2022 走看看