异常a different object with the same identifier value was already associated with the session 产生原因及解决
来源:互联网 发布:rsa密码算法 编辑:程序博客网 时间:2024/06/13 10:11
今天做毕设项目的时候,更新登录信息hibernateTemplate.update(entity); 时突然给我抛了个
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:
这个错误产生原因相信大家都知道,因为在hibernate中同一个session里面有了两个相同标识但是是不同实体.
即2个不同的对象关联到了同一个标志位,
也就是说在一个session中存在2个对象但这2个对象的标志位是相同的比如2个对象的 id值是一样的 而这又是数据库的唯一主键在做更新操作的时候就出现冲突了,
因为Hibernate不知道到底要去更新哪个对象出现这样的问题头都大了,后来 仔细分析了下,
在登录界面使用ajax调用了1次DAO层产生了一个dto对象,ajax验证完后进行登录信息修改再次调用1次DAO层,有产生了一个相同的id一样的dto,
在同一个业务逻辑方法中调了2次DAO层的方法,如果要修改的对象在
数据库中还存在就有个问题产生了,该方法获得的session管理了一个名为pro的对象而这个对象的id和方法传入的
product的id是一样的所以 当在本方法中再去调DAO层的modifyProduct方法的时候获得的session是和调
findProductByName这个业务方法获得的一个session是同一个 ,因为我把session的关闭操作放到页面请求
处理结束response回去的时候通过那个字符编码转化的filter过滤的是执行完doFilter方法后做的关闭
Hibernate Session操作来实现提交数据到数据库的 所以最终在这个修改的业务逻辑中使用2次Dao层的方法
而获得的是同一个session 所以要修改一个和该session管理的一个对象一样的identifier的对象会出现冲突.
解决办法有在该方法中执行hibernateTemplate.update(entity);前先将session关闭 调用hibernateTemplate.close();方法Session的缓存被清空,
其中的所有持久化对象都变为游离对象,调用Session的evict()方法能够从缓存中删除一个持久化对象,使其变为游离状态,
这样也能达到解决同一个session中关联2个同一个identifer的对象更新的时候产生的冲突
如下
- private HibernateTemplate hibernateTemplate;
- public HibernateTemplate getHibernateTemplate() {
- return hibernateTemplate;
- }
- @Resource
- public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
- this.hibernateTemplate = hibernateTemplate;
- }
- public void setLastLoginTime(UserDTO udto) throws Exception {
- User entity = new User();
- BeanUtils.copyProperties(udto, entity);
- hibernateTemplate.clear();
- /方法Session的缓存被清空, hibernateTemplate.update(entity);
- }
- 异常a different object with the same identifier value was already associated with the session 产生原因及解决
- a different object with the same identifier value was already associated with the session 产生原因分析
- a different object with the same identifier value was already associated with the session异常解决办法
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session错误
- 解决a different object with the same identifier value was already associated with the session错误
- 解决 a different object with the same identifier value was already associated with the session
- 解决a different object with the same identifier value was already associated with the session错误
- arguments
- Android与服务器端数据交互(基于SOAP协议整合android+webservice)
- 再谈Trie-字典树
- 一些博客
- SQL SERVER错误代码大全 .
- 异常a different object with the same identifier value was already associated with the session 产生原因及解决
- MySQL 替换新的数据文件 innodb
- AM3359 FATFS文件系统移植
- vs2008 静态编译 动态编译(release debug)
- 在MyEclipse中安装Freemarker插件
- Pl sql case语句应用实例
- 计算机编程类经典图书
- mysql
- HDOJ1162(Prim 或 Kruscal)