做项目的时候遇到一个很头疼的报错:Illegal attempt to associate a collection with two opensessions
出错原因: 一个Object Version 包含了两个oneTomany关系。在数据库调取出一个version对象时,再用saveOrUpdate这个对象就会出现如上错误
解决办法: 讲session.saveOrUpdate(version)改为session.merge(version)
原理分析: saveOrUpdate是在同一个session里用于保存或更新一个条目,而merge可以用于两个不同session数据的保存或更新。
HibernateReference里说:
saveOrUpdate() does the following:
- if the object is already persistent in this session, donothing
- if another object associated with the session has the sameidentifier, throw an exception
- if the object has no identifier property, save() it
- if the object's identifier has the value assigned to a newlyinstantiated object, save() it
- if the object is versioned by a or , and the version property valueis the same value assigned to a newly instantiated object, save()it
- otherwise update() the object
merge() is very different:
- if there is a persistent instance with the same identifiercurrently associated with the session, copy the state of the givenobject onto the persistent instance
- if there is no persistent instance currently associated with thesession, try to load it from the database, or create a newpersistent instance
- the persistent instance is returned
- the given instance does not become associated with the session, itremains detached
网上有其他资料说代码如下:entity=merge(entity); saveOrUpdate(entity);这是错误的,第二行代码完全没有必要,因为entity已经是persistentobject,会被saveOrUpdate忽略。直接用session.merge(version)即可。