解决org.hibernate.NonUniqueObjectException的问题
不知道是不是之前处理懒加载的问题对session工厂进行了处理,导致了之前没有问题的地方出现了错误.
当修改班级操作时出现了错误
前端错误信息

后台处理以及报错信息

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]
出现这个异常的原因:
Hibernate 增删改在 session 中已存在相同 OID(主键) 的对象。比如,先删除,后插入。
这就会报上面的错误。
原来是因为在Hibernate中同一个session内,如果已经有一个对象已经是持久化状态(load进来等),现在构造一个新的PO,和前一个持久化对象拥有相同的持久化标识(identifier),在update的时候,就会抛这个错误。
解决方法
1.不要重新new一个对象,使用load的对象对他进行更改值。
2.如果是hibernate3以上,可以使用session.merge()方法
3.把session中同标识的对象移出(session.evict(user1)),使他成为脱管的状态,然后user2就可以update了

this.getHibernateTemplate().getSessionFactory().getCurrentSession().clear();加上这行代码清除session后,执行通过
解决org.hibernate.NonUniqueObjectException的问题的更多相关文章
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session异常解决办法
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread ...
- org.hibernate.NonUniqueObjectException 原因及解决办法
问题 使用hibernate更新对象时,出现如下错误: org.hibernate.NonUniqueObjectException: a different object with the same ...
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was alread---------程序报错
今天遇到了这个问题: org.hibernate.NonUniqueObjectException: a different object with the same identifier value ...
- opensessioninviewFilter导致org.hibernate.NonUniqueObjectException
起因: 公司业务需求,增加了一个新的数据源,增加之后,起初一切正常,但是发现后台管理系统所有Ajax请求获取信息没有问题,但是涉及到保存操作就抛出异常. 异常: org.hibernate.NonUn ...
- org.hibernate.NonUniqueObjectException
错误如下: 2017-3-29 15:17:52~ERROR~org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV ...
- atitit. 解决org.hibernate.SessionException Session is closed
atitit. 解决org.hibernate.SessionException Session is closed #--现象:: org.hibernate.SessionException ...
- hibernate异常:org.hibernate.NonUniqueObjectException
异常:org.hibernate.NonUniqueObjectException 提示:a different object with the same identifier value was a ...
- Exception 06 : org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session :
异常名称: org.hibernate.NonUniqueObjectException: A different object with the same identifier value was ...
- org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
保存实体异常 https://blog.csdn.net/zzzz3621/article/details/9776539 org.hibernate.NonUniqueObjectException ...
随机推荐
- Docker构建ssh镜像
FROM ubuntu MAINTAINER ggzone xxx@live.com ENV REFRESHED_AT 2015-10-21 RUN apt-get -qqy update & ...
- 【phonegap】IOS按HOME键,程序进入suspended状态,再调出,界面出现文字丢失问题
发现丢失的都是时间类型的,把日期型改成 yyyy年mm月dd日 时间从冒号表示(12:00)改成中文表示,12点00分,就不再丢失了. 未找到真正原因
- 解决webpack环境变量NODE_ENV跨平台兼容性问题
为什么要用cross-env插件 学过webpack的人都知道,webpack可以使用NODE_ENV=development或者NODE_ENV=production这样的方式来设置全局变量 这样的 ...
- Python遍历列表删除多个列表元素
在遍历list的时候,删除符合条件的数据,结果不符合预期 num_list = [1, 2, 2, 2, 3] print(num_list) for item in num_list: if ite ...
- 分布式爬虫搭建系列 之二-----神器PyCharm的安装
这里我们使用PyCharm作为开发工具,以下过程摘抄于:http://blog.csdn.net/qq_29883591/article/details/52664478 作者:陌上行走 Pytho ...
- leetcode524
public class Solution { public string FindLongestWord(string s, IList<string> d) { string long ...
- swift之xib关联UIView
有点坑爹,设置file owner 不行,搞了一早上,来说下怎么关联吧 自定义UIView要重写 required init(coder aDecoder: NSCoder) { super.init ...
- java基础之匿名内部类
内部类: 概述: 类里边还有一个类, 里边那个类叫内部类, 外边那个类叫外部类. 分类: 成员内部类: 定义在成员位置的内部类. 局部内部类: 定义在局部位置的内部类. 格式: new 类名或者接口名 ...
- 【bzoj1056】排名系统
1056: [HAOI2008]排名系统 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2195 Solved: 623[Submit][Statu ...
- 【总结整理】关于房产app的比较
从切换城市的分类方式就能看出来,因覆盖城市很多,搜房网(房天下)跟安居客都用上了拼音选房,而链家因城市很少,只需简单罗列即可. 搜房网(房天下)覆盖城市多达651个,覆盖范围最广,安居客为500个,两 ...