`
xgdellis
  • 浏览: 62611 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

hibernate异常:different object with the same identifier value was already associat

阅读更多

近来一直在忙项目,项目用的是SSH。而且是在springside的基础上做开发。用过springside的朋友,都应该知道springside用的是paramsPrepareParamsStack这个神奇的拦截器的功能。我要讲的并不是这方面的东西,所以不再做详细介绍,直奔主题。

这两天我遇到了个非常让人摸不着头脑的异常“different object with the same identifier value was already associated with the session”,这是在使用继承于SimpleHibernateDao的Dao类的save操作时报出来的异常。

具体代码是:

   AdminAction.java:

....public String personalSave() throws Exception {
		adminManager.personalSave(admin);
		return "personal-reload";
	}

	//TODO 为什么有了这个方法不会报“a different object with the same identifier。。”这个错了
	public void preparePersonalSave() throws Exception {
		this.prepareModel();
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if (id != null && !id.equals("")) {
			admin = adminManager.findById(id);
		} else {
			admin = new Admin();
		}
	}........

 AdminManager.java

....
public void personalSave(Admin admin) {
		Admin a = this.findByLoginName(null);
		// TODO 测试list的equals
		if (admin.getId().equals(a.getId())
				&& admin.getLoginName().equals(a.getLoginName())
				&& admin.getPositions().equals(a.getPositions())) {
			adminDao.save(admin);
		}
	}
...

 

一开始,我并没有为AdminAction里的personalSave()方法写一个对应的preparePersonalSave()方法,当程序运行到AdminManager里的personalSave()方法里的adminDao.save(admin)后就会报出那个异常。一开始,这样我非常困扰,试过很多方法都不能解决。苦想许久后,灵机一动,心想会不会是少了preparePersonalSave()的问题,当我加上preparePersonalSave()后,问题果然解决了。

原来这个异常说的是session里存在id相同的不同对象,导致保存操作的时候hibernate不知道保存哪个。而通过prepareModel()方法取出来的对象和通过findByLoginName()方法取出来的对象在session里是同一个对象,所以不会造成这种冲突,自然也不会报这个异常了。

上面所说的prepareModel()和findByLoginName()方法是我自己写的从数据库里面查找出对象的方法,我想表达的意思是:用hibernate的get,load,query等方法分别查出来的对象,只要对应着数据库里的同一条数据,那么在hibernate的session里就是同一个对象

关于上面的东西纯属我个人理解思考后得出的结论,并不一定就是正确的,如果有错误指出欢迎指点。

0
0
分享到:
评论

相关推荐

    hibernate常见异常

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

    hibernate常见问题及处理.doc

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

    解决error C2065: '_beginthreadex' :undeclared identifier

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

    The Digital Object Identifier initiative: metadata implications

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

    Hibernate Tutorial 03 (Object Identifier).pdf

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

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

    hibernate常见错误解决方案

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

    Hibernate映射导致的几个异常

    标题与描述概述的知识点主要集中在Hibernate映射过程中可能遇到的各种异常情况,这涉及到数据库与对象模型之间的映射问题,以及Hibernate框架在处理这些映射时可能产生的错误。下面将对这些异常进行详细的解释和分析...

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

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

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: The height of the TFlexRegularPolygon object incorrectly changes with its rotation. - FIX: Added division by zero protect in method TFlexControl.MovePathSegment. - FIX: The background beyond ...

    hibernate错误解决方案

    org.springframework.orm.ObjectRetrievalFailureException: Object of class [com.xindeco.myregister.pojo.MyRegisterInfo] with identifier [14]: not found ``` 这表示尝试获取 `MyRegisterInfo` 类型的对象,但...

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

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

    snmp++使用封装

    snmp::ObjectIdentifier oidObj(myOid, sizeof(myOid) / sizeof(oid)); ``` 4. **执行snmpget**:使用`session.get()`方法执行SNMP GET操作。 ```cpp snmp::ErrorStatus error; snmp::VariableList vl; error = ...

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

    In this scenario, since T1 believes it locks the entire table, it might inadvertently make changes to the same row that T2 thought it has locked exclusively. In a multigranular locking environment, ...

    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的实体...

    将tc下的程序移植到vc中.pdf

    8. **错误提示:error C2065: 'EGA' : undeclared identifier 或 error C2065: 'EGAHI' : undeclared identifier** 解决办法:删除定义`int gdriver = EGA, gmode = EGAHI;`的代码,改用`initgraph(640, 480);`初始...

    Hibernate4

    - 加载:根据特定的 OID(Object Identifier,对象标识),从数据库加载单个对象到内存中。 为了实现这些操作,每个对象都需要有一个唯一的标识号,即 OID。在关系数据库中,这个标识通常被称为“主键”。 #### ...

    4.2.2 variables_to_restore函数的使用样例.ipynb

    TensorFlow资源以及具体的用法

Global site tag (gtag.js) - Google Analytics