近来一直在忙项目,项目用的是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里就是同一个对象。
关于上面的东西纯属我个人理解思考后得出的结论,并不一定就是正确的,如果有错误指出欢迎指点。
分享到:
相关推荐
报错误error C2065: '_beginthreadex' :undeclared identifier
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 ...
Microsoft OLE DB Provider for ODBC Drivers 错误'80040e4d'[Oracle][ODBC][Ora]ORA-12154: TNS:could not resolve the connect identifier specified. 使用PL-SQL Developer能连接上Oracle,但是ASP+ODBC连不上...
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 ...
本文介绍Oralce数据库中,表名可以使用的最大长度。这个长度也是Oracle...*Cause: An identifier with more than 30 characters was specified. *Action: Specify at most 30 characters. 从这个错误提示可以看出
上web tools课时老师发的关于hibernate的教程之一。大概内容看标题就知道了。需要注意的是文档全部是英文的。英文不好的童鞋就不要下了。。。
- 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 ...
- 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 ...
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 ...
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 ...
fe ge 2007 programe
Error 237:’LedOn’:function already has a body 说明 LedOn( )函数名称重复定义 即有两个以上一样的函数名称 解决方法 修正其中的一个函数名称 使得函数名称都是独立的 5 ***WARNING 16:UNCALLED SEGMENT,...
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, ...
TensorFlow资源以及具体的用法
A clustered index is like a telephone directory in which all of the rows for customers with the same last name are clustered together in the same part of the book. Just as the organization of a ...
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...
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 ...
## 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....
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 ...
Value: The fully-qualified identifier for the control. /// /// ///Occurs on the client side after the control has been initialized but prior to it being displayed within the browser. /// };