最近的一个项目使用Hibernate3和Spring,DAO都是继承自
org.springframework.orm.hibernate3.support.HibernateDaoSupport,提供了一个
BaseDAO,提供了常用的get,getAll,insert,delete以及update方法,其它的DAO都不用关心这些操作了,但是在使用的
时候有一个问题,就是有时候会出现Illegal attempt to associate a collection with two open sessions的异常,但是在取得对象后使用session.close()也不行,会出现另外的一个异常session is not open or is closed,后来上网查了以下,原来是我的update方法有问题,原来就是getHibernateTemplate().update(object),修改为getHibernateTemplate().merge(object)问题解决。
2005.11.23更新:
按照API文档:
merge(Object entity)
Copy the state of the given object onto the persistent object
with the same identifier.
Update the given persistent instance,
associating it with the current Hibernate Session.
问题的根源可能在于要更新的那个对象是有一个关联集合的。