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)即可。

  • 相关阅读:
    c# 自定義事件
    c# 判斷事件中鼠標的左右按鍵
    I swear(我發誓)
    SQL Server实用操作小技巧集合
    富人和穷人的经典差异
    Mssql入门语句
    c# 匿名方法學習
    SOS: Autodesk MapGuide Studio Preview can not display "Feature Label" with Chinese text on the platform of MapGuide Open Source
    MapGuide open source开发心得二: 资源
    moblie development based on .net compact framework2 solution:activeX
  • 原文地址:https://www.cnblogs.com/codingforum/p/4316865.html
Copyright © 2011-2022 走看看