最近遇到的一个问题

当我们使用hibernatetemplate的saveOrUpdateAll 方法,在数据量达到一定程度时,会必然出现如下的错误:

“identifier of an instance altered from X to Y”?

这是很奇怪的,

问题【1】该方法实现是这个样子的:

public void saveOrUpdateAll(final Collection entities) throws DataAccessException {
768         executeWithNativeSession(new HibernateCallback() {
769             public Object doInHibernate(Session session) throws HibernateException {
770                 checkWriteOperationAllowed(session);
771                 for (Iterator it = entities.iterator(); it.hasNext();) {
772                     session.saveOrUpdate(it.next());
773                 }
774                 return null;
775             }
776         });
777     }

如果真的是这样的话,为什么还会出现上述的错误?

从上面的方法看就是一个一个的迭代执行,而这样与自己迭代执行有什么区别? 为什么这个会报错?

问题【2】经测试,单独一次提交1000,提交多次 是不会出现该问题得。这是问什么?

性能方面1w条-》152s 确实较慢 后面试一下直接使用jdbc卡看需要多久。

TO BE CONTINUED .....[代码随后附上]

https://github.com/gaoxingliang/algintroduction/tree/master/src/hibernate

update:hibernate4 兼容性有点问题,没有找到updateAll方法,可能正如下面的链接中说说的可能已经删除了。

下面这些链接会有帮助:

http://stackoverflow.com/questions/4179166/hibernate-how-to-fix-identifier-of-an-instance-altered-from-x-to-y

http://forum.spring.io/forum/spring-projects/data/56941-problem-with-saveorupdateall-method  这里说了如何替代该方法,就是在迭代器中单独调用saveOrUpdate

http://forum.spring.io/forum/spring-projects/data/113874-hibernatetemplate-saveorupdateall-collection-entities-performance 这里描述它的性能,2000/15s 与实际一致。

http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-orm/2.5.6/org/springframework/orm/hibernate3/HibernateTemplate.java#767

PS:

从这里下载打好的spring zip包:http://maven.springframework.org/release/org/springframework/spring/3.2.4.RELEASE/

hibernate3:http://sourceforge.net/projects/hibernate/files/hibernate3/3.5.0-Final/hibernate-distribution-3.5.0-Final-dist.zip/download?use_mirror=nchc

转载于:https://www.cnblogs.com/scugxl/p/4131791.html

Spring or hibernate saveOrUpdateAll 偶然失效相关推荐

  1. Spring Spring MVC Hibernate 整合备忘

    以下为此三种框架整合配置的详细备注,以及部分问题备忘 项目结构和配置文件可访问 Github 查看 1. pom.xml 尽量使用 Maven 管理项目依赖以减少包引入时的麻烦,以及避免跨开发工具问题 ...

  2. ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存

    ssh整合hibernate 使用spring管理hibernate二级缓存,配置hibernate4.0以上二级缓存 hibernate  : Hibernate是一个持久层框架,经常访问物理数据库 ...

  3. 【spring框架】spring整合hibernate初步

    spring与hibernate做整合的时候,首先我们要获得sessionFactory. 我们一般只需要操作一个sessionFactory,也就是一个"单例",这一点很适合交给 ...

  4. Spring MVC + Hibernate JPA + Bootstrap 搭建的博客系统

    Spring MVC + Hibernate JPA + Bootstrap 搭建的博客系统 Demo 相关阅读: 1.Spring MVC+Hibernate JPA+ Bootstrap 搭建的博 ...

  5. Spring+SpringMvc+Hibernate 框架搭建

    2019独角兽企业重金招聘Python工程师标准>>> 框架篇:Spring+SpringMVC+hibernate整合开发 一.建立项目 1.新建一个空项目project 相当于e ...

  6. 在Spring、Hibernate中使用Ehcache缓存

    前一篇http://blog.csdn.net/ibm_hoojo/article/details/7739181介绍了Ehcache整合Spring缓存,使用页面.对象缓存:这里将介绍在Hibern ...

  7. 【Spring】Spring系列6之Spring整合Hibernate

    6.Spring整合Hibernate 6.1.准备工作 6.2.示例 com.xcloud.entities.book com.xcloud.dao.book com.xcloud.service. ...

  8. Spring整合Hibernate图文步骤

    工具:myeclipse9.0 Spring版本:2.5.6 Hibernate版本:3 昨天花了一下午时间把Spring和Hibernate整合到了一起,今天做一个笔记. 首先建立java Proj ...

  9. spring入门(11)-spring与hibernate整合完成增删改查的操作(封装HibernateTemplate模版类对象)

    今天是spring的最后一节课,这节课老师讲了spring与hibernate整合完成增删改查的操作,这是很重要的一节课,这也是第一次真正的实现spring结合Hibernate和数据库连接上,下面是 ...

最新文章

  1. T-SQL with as 关键字
  2. turtle 20秒画完小猪佩奇“社会人”
  3. 如何简单快速调试高大上的谷歌浏览器
  4. python2.7 与 go1.2简单性能比较
  5. H3C认证云计算高级工程师
  6. 计算机专业考研是哪个学科,我是大二计算机专业女生 想考研 但对未来的考研专业感到很迷茫 不知道都有什么方向 并且每个专业方向的......
  7. 倍福--控制步进电机
  8. 浪潮服务器一键还原系统,一键恢复 怎样做备份
  9. android拖拽 字体变形,字体变形的几种方法与技巧
  10. 云控微信开发SDK使用教程--手机微信收钱任务执行结果通知服务端
  11. Python错误集锦:除法运算时提示ZeroDivisionError: division by zero
  12. 凑热闹,列个非主流书单:(1)分析与解决问题、通用管理(2)技术管理/CTO...
  13. 《数据库原理实验指导书》
  14. 全球通胀对IT领域的影响
  15. 西安工业大学计算机学院研究生招生简章,西安工业大学计算机技术在职研究生招生简章...
  16. ionic的生命周期
  17. BootstrapTable去掉表格与分页的空白
  18. 十一、决策树和随机森林
  19. 如何解决电脑无法访问个别网站
  20. FP-growth:从FP树中挖掘频繁项集

热门文章

  1. 【干货】大中型企业网络搭建
  2. 【送】VMware 虚拟化知识思维导图
  3. html手机端页面meta,手机页面的 HTMLmeta 标签使用与说明
  4. 中国科协发布 2021 开源创新榜,阿里巴巴 2 大开源社区、5 大开源项目上榜
  5. 阿里云 EventBridge 事件驱动架构实践
  6. GPU 机器学习开箱即用
  7. 如何在 Knative 中部署 WebSocket 和 gRPC 服务?
  8. linux线程创建 类函数吗,linux多线程创建时使用类成员函数作为参数
  9. APP元素获取信息操作API
  10. java 模仿支付成功_揭秘Java工程师薪资为何这么高,教你如何成为架构师拿50W年薪!...