在使用hibernate框架里面的:saveOrUpdate报错:

org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was

 

意思就是另一个对象的id(id同值)已经被session关联了。

原因分析:

org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was

在第1步中中通过titleList.get(0)获取一个对象duty1,在第2步中将duty的dutyId设置成duty1的dutyId,而duty1本来就是存在一级缓存session里面的,在第3步时saveOrUpdate(duty)相当于内存中存在两个dutyId一样的对象,因此抛出异常。

解决方法:

如果dao或Service类继承了HibernateDaoSupport,可以调用this.getHibernateTemplate().clear();清除session(的关联)。

如图中的第二步添加了:this.getHibernateTemplate().clear();

 

相关文章:

  • 2022-12-23
  • 2021-10-14
  • 2022-02-09
  • 2022-01-19
  • 2021-08-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-17
  • 2021-10-01
  • 2021-10-20
相关资源
相似解决方案