错误:hibernate级联保存,报错A different object with the same identifier value was already associated with the session
解决方案:使用getHibernateTemplate().merge();方法来保存
原因:从上下文来看,这里在保存前先去数据库中取了一次数据(放session缓存里),然后在保存的。根据hibernate的官方文档里,这样描述update的:
Update the persistent instance with the identifier of the given detached instance,if there is a persistent instance with the same identifier,an exception is thrown.所以上面的错误就很好得被解释了。
同样,hibernate的merge方法的解释是:copy the state of the given object onto the persistent object with the same identifier,if there is no persistent instance currently associated with the session,