做项目用到hibernate 对数据进行持久化处理,在做对象修改的时候突然跳出a different object with the same identifier value was already associated with the session 异常。
本来就是下午 ,头晕晕的,突然来个这错误,那就更晕了,先不管三七二十一,将异常放到谷歌里一搜,
看了很久,找到一个说要进行session.clean()操作,但在那操作,却不清楚,
要知道项目里面用到session的地方很多,但是却给了我一个线索,从异常来看,
大只是说有session中有两个本应该是唯一标识的实体,就是说session中实体有重复,
那么这是怎么重复的呢?在哪里开始session中便有了两个相同实体了呢?
回想在先前做的也是一个对对象进行修改的操作中,却没有出现这个异常,
于是与之比较,终于发现问题,原来在获得这个要修改的对象时,根据id先获得对象,代码如下:
public WebSite getUpdateWebSite(int webId) {
Session session=HibernateSessionFactory.getSession();
return (WebSite) session.get(WebSite.class, webId); }
将得到的对象经过用户操作进行修改,然后传回action调用业务逻辑层进行 保存修改操作时
代码:
public void updateWebSite(WebSite web) {
Session session=HibernateSessionFactory.getSession();
Transaction tr=session.beginTransaction();
try{ session.update(web); tr.commit(); }
catch(Exception e){
tr.rollback();
e.printStackTrace();
} finally{
session.close();
}
}
这时便出现了上面的异常,你仔细观察便发现 ,当我用id获得对象时
是直接用return (WebSite) session.get(WebSite.class, webId);
这时 session并没有关闭,也就是说此时通过session获得的对象仍是持久化的,由于session没有关闭 ,
当我们后来将用户修改后的此对象传入业务层进行持久化操作时,此对象的id ,用户是改变不了的 ,
他通常 放在jsp页面中的一个隐藏域中,所以当再次对他进行修改操作时,
便会出现session中已有一个这样的对象,也就是上面的异常。
知道了这个,那么修改起来就简单了 ,只要把用id获得对象方法中的session关掉就ok了;
public WebSite getUpdateWebSite(int webId) {
Session session=HibernateSessionFactory.getSession();
WebSite web=(WebSite) session.get(WebSite.class, webId);
session.close();
return web;
}
分享到:
相关推荐
a different object with the same identifier value was already associated with the session 是一种常见的异常,通常是由于在 Hibernate 中同一个 session 里面有了两个相同标识但是是不同实体所致。解决方案有...
5. `a different object with the same identifier value was already associated with the session` 这个异常表明在同一个Hibernate Session中存在两个具有相同标识但实际是不同对象的情况。处理方法有: - 使用`...
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 ...
数字对象标识符(Digital Object Identifier,简称DOI)是一种持久性标识符,用于唯一地标识数字资源,如学术文章、数据集等。DOI系统由国际DOI基金会管理,并在出版界广泛采用。随着数字资源的增加以及对资源描述的...
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 ...
- 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 ...
- 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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
created with the default settings in Microsoft Visual C++ 6 with one major exception: aside from other compiler and linker flags, the executable in the DEBUG directory has been compiled with the ...
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 ...
make use of the CAN implementations that are already available on the market, or of new controllers that implement both formats. In order to distinguish standard and extended format the first reserved...
本文介绍Oralce数据库中,表名可以使用的最大长度。这个长度也是Oracle...*Cause: An identifier with more than 30 characters was specified. *Action: Specify at most 30 characters. 从这个错误提示可以看出
Stores a unique identifier for the file, allowing for the identification of files across different volumes. ##### Attribute - $SECURITY_DESCRIPTOR (0x50) Holds security-related information, such as ...
LibTomCrypt is a fairly comprehensive, modular and portable cryptographic toolkit that provides developers with a vast array of well known published block ciphers, one-way hash functions, chaining ...