今天在做一个更新操作时,出现一个错误,内容如下:
org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was
already associated with the session: [cn.guopeng.oa.model.User#13];
nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [cn.guopeng.oa.model.User#13]
service层的java代码:
dao层的代码:
测试dao层通过,但在service层的测试就出现了上述错误。原因应该是:在根据id获得person对象时,同时获得了一个user对象,因为user和person是一对一关系,所以获得的user对象与要修改的user对象就有了同样的id——13,而且在做修改时还是先前的session,这时作修改操作就会出错。办法就是要清空下session之后再做update,于是在dao层update之前,插入代码:hibernateTemplate.clear(); ,搞定!