`
leobluewing
  • 浏览: 238773 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

Hibernate常见错误 a different object with the same identifier value was already

阅读更多
从开始接触hibernate起

a different object with the same identifier value was already associated with the session

这个错误就一直时不时出现,以前自己也研究过,也经常被同事经常问起。这两天又被问了,干脆记一下。

错误原因很简单,就是英文的字面意思。hibernate不知道保存哪个对象。

那么到底是如何导致的呢?从头说起。

hibernate生命周期有三态,Transient(瞬时),Persist(持久) 和 Detached(游离)。

简单来看报错的场景:
Object obj = new Object(); //obj是瞬时态

obj.setId("xxxxxxxxx"); //如果new的时候给了Id,那么为游离态

Object obj1 = session.get(Object.class,"xxxxxxxxx"); //从数据库里加载一个obj,为持久态。


所以,当内存里同时有obj 和 obj1存在的情况下,如果你执行了

session.saveOrUpate(obj); //注意是obj 不是 obj1


那么hibernate将会把obj转变为持久态,但是此时内存里有一个持久态的obj1。于是报错了。


以上是原理,那么一般是怎么引起这种情况的呢? 从前台说起。

web提交的一般过程如下:form --> servlet

数据从form提交到后台时,在页面上都是以字符串形式存在,那么当后台我们需要处理的时候

不可能写这样的代码


public void doMybiz(String id,String name,String value1,String value2...){
         ..
}


一定是希望直接获得对象来处理

public void doMybiz(Object obj){
         ..
}


所以,很多的前端框架都做了这样的事情,即将页面上的数据组装成对象给后台。

一般来说,都是直接new出来的,然后进行赋值。

so,前面说的游离态对象出来了。所以如果你在你的业务方法里,额外查询了一次的时候,就会报错了。




分享到:
评论

相关推荐

    The Digital Object Identifier initiative: metadata implications

    The Digital Object Identifier initiative: metadata implications Context/status information The purpose of this document is to generate a DOI Foundation consensus paper on metadata, as a companion ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: The value of some string flex-properties that began with a parenthese or curly bracket had no apostrophe at the end; that caused an error when reading. (fixed TPropList.SavePropValue for the ...

    S7A驱动720版本

    - When the signal conditioning function "S&M" was used for a byte value, the driver wrote wrong values to the PLC. Build 223 : New Functions: - The number of supported NetLinks has been ...

    Hibernate Tutorial 03 (Object Identifier).pdf

    上web tools课时老师发的关于hibernate的教程之一。大概内容看标题就知道了。需要注意的是文档全部是英文的。英文不好的童鞋就不要下了。。。

    计算机网络第六版答案

    In these systems, packets are transmitted over the same wireless infrastructure used for cellular telephony, with the base station thus being managed by a telecommunications provider. This provides ...

    微软内部资料-SQL性能优化3

    Another type of table lock is a schema stability lock (Sch-S) and is compatible with all table locks except the schema modification lock (Sch-M). The schema modification lock (Sch-M) is incompatible ...

    NewSID(光学习一下代码就可以了,没看清楚介绍别运行)

    When the SID is found in a value it is replaced with the new computer SID, and when the SID is found in a name, the key and its subkeys are copied to a new subkey that has the same name except with ...

    微软内部资料-SQL性能优化5

    Each index row in node pages contains an index key (or set of keys for a composite index) and a pointer to a page at the next level for which the first key value is the same as the key value in the ...

    Hibernate Reference Documentation3.1

    10.9. Replicating object between two different datastores 10.10. Flushing the Session 10.11. Transitive persistence 10.12. Using metadata 11. Transactions And Concurrency 11.1. Session and transaction...

    servlet2.4doc

    Adds a response header with the given name and value. addIntHeader(String, int) - Method in class javax.servlet.http.HttpServletResponseWrapper The default behavior of this method is to call ...

    VclZip pro v3.10.1

    This was due to a problem where it would be freed automatically if there was a problem with the ArchiveStream when trying to open it as a zip file (possibly corrupt). Best practice is that ...

    developing android application with adobe air.part3.rar

    EXIF Data and the Map Object The speed Property Conclusion Chapter 11 : Microphone and Audio The Microphone Audio Assets Working with Sounds ID3 Tags Modifying Sound Raw Data and the Sound Spectrum ...

    hibernate.properties

    ## to use the second-level cache with JTA, Hibernate must be able to obtain the JTA TransactionManager #hibernate.transaction.manager_lookup_class org.hibernate.transaction....

    Oracle数据库表名支持的最大长度是多少

    本文介绍Oralce数据库中,表名可以使用的最大长度。这个长度也是Oracle...*Cause: An identifier with more than 30 characters was specified. *Action: Specify at most 30 characters. 从这个错误提示可以看出

    高性能消息传输机制Aeron.zip

    two and must be the same length on both ends.Term ID: A unique identifier for a Term within a Stream. Starts randomly. Must increase monotonically. Can wrap around. Can not go back to a wrapped ...

    Hibernate+中文文档

    10.1. Hibernate对象状态(object states) 10.2. 使对象持久化 10.3. 装载对象 10.4. 查询 10.4.1. 执行查询 10.4.2. 过滤集合 10.4.3. 条件查询(Criteria queries) 10.4.4. 使用原生SQL的查询 10.5. 修改...

    Devart LinqConnect 3.1

    The "Identifier Case Sensitive" check box, which enables case-sensitive processing of database object identifier names, is added to MySQL Server Options The documentation generation for Enum members ...

    windows程序设计

    This CD contains both the source code for every program discussed in the book as well as executable files. You can open the source files in the Microsoft Visual C++ environment or in your favorite ...

    hibernate3.2中文文档(chm格式)

    HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...

    HibernateAPI中文版.chm

    HIBERNATE - 符合Java习惯的关系数据库持久化 Hibernate参考文档 3.2 -------------------------------------------------------------------------------- 目录 前言 1. 翻译说明 2. 版权声明 1. Hibernate...

Global site tag (gtag.js) - Google Analytics