1、
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.
Copy the state of the given object onto the persistent object with the same identifier.
2、update(Object entity)
Update the given persistent instance, associating it with the current Hibernate Session.
参考文档:如果你确定当前session没有包含与之具有相同持久化标识的持久实例,使用update()。 如果想随时合并你的的改动而不考虑session的状态,使用merge()。
我的理解:update只能在保证在session中只有一个持久化标示时,可以使用update,如果你在从数据库或者其他地方得到了另一个持久化对象,标示跟以前的某个对象一样,这个时候,只能用merge
错误情况:使用update有时候会出现
Illegal attempt to associate a collection with two open sessions的异常,但是在取得对象后使用session.close()也不行,会出现另外的一个异常
session is not open or is closed
,原来就是getHibernateTemplate().update(object),修改为getHibernateTemplate().merge(object)问题解决。