`

解决hibernate: a different object with the same identifier value was already assoc

 
阅读更多

今天做项目想更新update一个对象的时候碰到了这个问题,一直查找是什么原因,在网上看到一些错误的原因是

因为hibernate中同一个session里面有了两个相同标识但是是不同实体

一直报下面的错误:
a different object with the same identifier value was already associated with the session。。
查了半天,刚开始发现别人建表没有给建主键,后来我加上了,以为自己加错了,弄了半天,还是一直报错,最后用的下面的第三种方法解决的,一下就好了。。,我就是用的hibernate3

网上的解决方法:

    解决方法一:session.clean()
    PS:如果在clean操作后面又进行了saveOrUpdate(object)等改变数据状态的操作,有可能会报出"Found two representations of same collection"异常。
    
    解决方法二:session.refresh(object)
    PS:当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为该方法是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下。
    
    解决方法三:session.merge(object)
    PS:Hibernate里面自带的方法,推荐使用。

clean,flush等方法都试过了,好像不是很有效,我用的是hibernate3.2,struts2.6,spring2.5,可能是环境的问题,最后用第三种方法实现的,重启解决了这个郁闷的难题。

 

代码:

@SuppressWarnings("unchecked")
 public boolean update(T entity) {
   T object = (T) getHibernateTemplate().merge(entity); //在更新前把这个实体先合并一下
   getHibernateTemplate().update(object);
  return true;
 }

 

如果其他的crud碰到了类似的问题,也应该这样来解决!hibernate3就用merge来解决!

分享到:
评论

相关推荐

    hibernate常见异常

    、org.hibernate.id.IdentifierGenerationException 以及 a different object with the same identifier value was already associated with the session 等。 1. net.sf.hibernate.MappingException ...

    解决error C2065: '_beginthreadex' :undeclared identifier

    ### 解决Error C2065:'_beginthreadex':Undeclared Identifier #### 错误概述 在编译过程中遇到“Error C2065:'_beginthreadex':undeclared identifier”这一错误信息时,通常意味着编译器无法识别`_...

    hibernate常见问题及处理.doc

    5. `a different object with the same identifier value was already associated with the session` 这个异常表明在同一个Hibernate Session中存在两个具有相同标识但实际是不同对象的情况。处理方法有: - 使用`...

    The Digital Object Identifier initiative: metadata implications

    数字对象标识符(Digital Object Identifier,简称DOI)是一种持久性标识符,用于唯一地标识数字资源,如学术文章、数据集等。DOI系统由国际DOI基金会管理,并在出版界广泛采用。随着数字资源的增加以及对资源描述的...

    hibernate常见错误解决方案

    **Different Object with Same Identifier Value** **异常描述:** 此异常表示同一个Session中存在具有相同标识符的不同对象实例。 **解决方法:** - 使用`session.clear()`清除Session缓存。 - 调用`session....

    Hibernate Tutorial 03 (Object Identifier).pdf

    在本教程中,我们将深入探讨Hibernate框架中的一个核心概念——对象标识符(Object Identifier)。通过本篇教程的学习,您将了解到对象标识符在Hibernate中的重要性以及如何正确地为持久化对象设置标识符。此外,我们...

    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 ...

    计算机网络第六版答案

    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 ...

    hibernate错误解决方案

    ### Hibernate 错误解决方案 #### 一、概述 在使用Hibernate框架进行开发的过程中,经常会遇到各种各样的异常问题。这些问题可能会导致程序无法正常运行,甚至影响到系统的稳定性。本文将针对几个常见的Hibernate...

    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 ...

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

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

    Python-KaggleInstacart市场篮子分析

    days_since_prior: days since the last order, capped at 30 (with NAs for order_number = 1) products (50k rows): product_id: product identifier product_name: name of the product aisle_id: foreign ...

    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 ...

    微软内部资料-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 ...

    高性能消息传输机制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错误汇总

    错误八:Exception in thread "main" org.hibernate.HibernateException: More than one row with the given identifier was found: 1, for class: org.model.User 错误原因:这个错误表示在尝试获取主键为1的实体...

    微软内部资料-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 ...

    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 ...

    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...

    Hibernate映射导致的几个异常

    Not-null property references a null or transient value 当Hibernate尝试保存一个实体时,如果该实体中的某个被标记为`@NotNull`或在映射文件中定义为`not-null="true"`的属性值为null或者指向一个尚未持久化的...

Global site tag (gtag.js) - Google Analytics