问题描述:
Hibernate调用update(object)或者saveOrUpdate(object)的时候出现异常:
a
different object with the same identifier value was already associated
with the session
问题原因:
在update(object)之前根据id查询出数据库的记录,但是在最后调用update(object)的时候传入的object不是数据库查询出来的对象,所以出现上述异常,主要是
因为在hibernate中同一个session里面有了两个相同标识但是是不同实体
解决方案:
如果查询出来数据库记录,update(object)的时候就必须是update数据库查出来的对象,至于属性值的更新,可以调用
org.springframework.beans.BeanUtils的
copyProperties(Object source, Object target, String[] ignoreProperties)
这个方法是把source的全部属性赋值给target,ignoreProperties是不进行赋值的属性数组。