`
fantaxy025025
  • 浏览: 1278733 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

Hibernate_Error_a different object with the same identifier value in session

阅读更多

Hibernate_Error:

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

 

又碰到这个问题,编程的时候早忘记了,自己先前已经把一个相同Id的entity加载进了内存。

 

导致错误的过程分析:

1) 自己先前因为某种原因已经把一个entity跟session关联上了

      关联的方式和内容有:

    A. 直接根据Id把entity加载进内存留待使用

    B. Update一个entity,但是update之后,此entity仍旧在内存中,留待使用

2) 又来了一个新的entity,Id和先前的相同,内容不同

3) session.update(entity) 报错了(注意update的同时也要把此entity和session关联)

    原因:session不知道该去持久化哪个entity

 

弄清楚了原因,解决方法其实可以千变万化,不必拘泥于网上Google来的各种解决方法。

 

另外需要很好的理解Hibernate中的对象类型,以及其转化条件和关系。

 

另外,有的网友说用Hibernate的session.merge()方法不好,好像用框架自己的方法就是不好一样。其实不是不好,而是需要理解merge究竟做了些什么。

Copy the state of the given object onto the persistent object with the same identifier. If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge" .

看,merge帮我们做了这么多事情。[哈哈,的确不好]

 

 

 

分享到:
评论

相关推荐

    hibernate常见异常

    a different object with the same identifier value was already associated with the session 是一种常见的异常,通常是由于在 Hibernate 中同一个 session 里面有了两个相同标识但是是不同实体所致。解决方案有...

    hibernate_persistence第02-16章书中源码

    《hibernate_persistence》是关于Hibernate持久化框架的一本书,涵盖了从第2章到第16章的源代码示例。这些章节深入探讨了如何使用Hibernate进行对象关系映射(ORM),并提供了作者亲自编写的代码实例,方便读者学习...

    Hibernate_v5.2.13.zip

    5. **Session(Session)**:这是Hibernate的核心接口,用于在应用程序和数据库之间建立会话。通过Session,我们可以执行CRUD操作,进行查询等。 6. **Query和Criteria API**:提供对数据库的查询操作,包括HQL...

    hibernate常见问题及处理.doc

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

    Hibernate Core 3.3.2.GA API+Hibernate_3.2.0_符合Java习惯的关系数据库持久化_zh_CN

    3. **Entity和Identifier**:Hibernate通过@Entity注解标记实体类,代表数据库中的表。实体类的主键由@Id注解标识,用于映射到数据库表的主键字段。 4. **Query和Criteria API**:Hibernate提供了两种查询方式,一...

    Hibernate_二级缓存总结

    **Hibernate 二级缓存详解** 缓存是一种提升应用程序性能的技术,它通过将常用数据存储在内存中,减少了对持久层数据库的访问,从而提高系统响应速度。在Hibernate框架中,缓存主要分为一级缓存和二级缓存。 **1. ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

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

    hibernate_reference中文文档.pdf

    ### Hibernate Reference Documentation 知识点概述 #### 1. 教程 ##### 1.1 第一部分 — 第一个 Hibernate 应用程序 该章节详细介绍了如何创建并运行一个简单的 Hibernate 应用程序。 - **1.1.1 设置**:这部分...

    hibernate_api

    Hibernate是一个开源的对象关系映射(ORM)框架,它允许Java开发者在Java应用程序中处理数据库操作,而无需直接编写SQL语句。Hibernate通过提供一个中间层,将数据对象与数据库表进行映射,使得数据访问更加方便、...

    hibernate常见错误解决方案

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

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

    hibernate _annotation 注解编程

    ### Hibernate Annotation注解编程知识点详解 #### 一、概述与设置环境 - **概述**:Hibernate 是一个流行的 Java 持久层框架,它提供了一种面向对象的方式来处理数据库操作。Hibernate 支持多种元数据定义方式,...

    Hibernate5.0用户手册中文版本

    POJO(Plain Old Java Object)模型是 Hibernate 的默认域模型,要求实体对象满足以下几点: 1. 实现无参构造函数: 实体对象必须提供一个无参构造函数。 2. 提供 identifier(标识)属性: 实体对象必须提供一个...

    servlet2.4doc

    Returns the object bound with the specified name in this session, or null if no object is bound under the name. getAttributeNames() - Method in interface javax.servlet.ServletContext Returns an ...

    最常见的VC++编译错误信息_fatal_error

    18. Error C4716:'CMyApp::InitInstance' : must return a value 这是一个常见的编译错误,原因是“CMyApp::InitInstance”函数必须返回一个值。解决方法是检查函数的返回值是否正确。 19. LINK : fatal error ...

    计算机网络第六版答案

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

    S7A驱动720版本

    whether the driver is running in the demo mode or in the licenced mode (with a valid key). - New control tag: "!RuntimeMode:S7A". A digital (i.e. DI) or analog (i.e. AI) block with the I/O ...

    The Digital Object Identifier initiative: metadata implications

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

Global site tag (gtag.js) - Google Analytics