项目中遇到这个 HibernateException: Illegal attempt to associate a collection with two open sessions.感觉很奇怪,本来就只在service中做一次查询,一次save操作。

一查代码发现是可能因为在service中存在两个不同的hibernate session都同时引用了同一个对象,一个是load(),一个是saveOrUpdate().

后面发现查询的session没有close,然后在hibernate dao 方法的finally中close掉session,就解决这个问题了