不知道是否是以前处理懒加载的问题对session工厂进行了处理,致使了以前没有问题的地方出现了错误.前端
当修改班级操做时出现了错误spring
前端错误信息session
后台处理以及报错信息app
16:37:36,034 ERROR ExceptionMappingInterceptor:38 - a different object with the same identifier value was already associated with the session: [cn.peiying.domain.Classes#128]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [cn.peiying.domain.Classes#128]
org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session: [cn.peiying.domain.Classes#128]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [cn.peiying.domain.Classes#128]dom
出现这个异常的缘由:ide
Hibernate 增删改在 session 中已存在相同 OID(主键) 的对象。好比,先删除,后插入。this
这就会报上面的错误。hibernate
原来是由于在Hibernate中同一个session内,若是已经有一个对象已是持久化状态(load进来等),如今构造一个新的PO,和前一个持久化对象拥有相同的持久化标识(identifier),在update的时候,就会抛这个错误。对象
解决方法blog
1.不要从新new一个对象,使用load的对象对他进行更改值。
2.若是是hibernate3以上,能够使用session.merge()方法
3.把session中同标识的对象移出(session.evict(user1)),使他成为脱管的状态,而后user2就能够update了
this.getHibernateTemplate().getSessionFactory().getCurrentSession().clear();加上这行代码清除session后,执行经过