通过WarehouseInventoryPreLog warehouseInventoryPreLog = new WarehouseInventoryPreLog();产生一个id序列

如果不flush(),那么数据库里就暂时没有这个值

-->hibernate的实体都是存储在缓存中的,
   所以你会发现有的时候当你创建出两个主键相通的实体的时候会报错。
   正常情况是当你调用save方法的时候,这个实体对象未必已经保存到数据库了,
   调用close方法的时候,对象才真正保存如数据库。当你调用flush方法的时候是强制将对象保存到数据库。
	if ("xs005".equalsIgnoreCase(logisticsOrderType.getTypeCode())
|| "xs001".equalsIgnoreCase(logisticsOrderType.getTypeCode())) {
// 锁定库存预分配,物流单类型是选择仓库的。
Long warehousId = logisticsOrders.getFromWarehouseId().getId();
Long customerId = logisticsOrders.getCustomerInfoByOwnerId()
.getId();
//callProcedureManualArrange(warehousId, customerId, newStr); //here is the problem !!!!
WarehouseInventoryPreLog warehouseInventoryPreLog = new WarehouseInventoryPreLog();
this.hibernateTemplate.save(warehouseInventoryPreLog);
this.hibernateTemplate.flush();
Long preLogId = warehouseInventoryPreLog.getId();
callProcedureManualArrangeForPreLog(warehousId, customerId, newStr, preLogId);
}

一个好几天到问题,hibernate不能读到高位值,导致锁表,原因是数据库产生主键值 和hibernate的hilo机制不同步,要在调用完存储过程后使用 hibernateTemplate.getSessionFactory().getCurrentSession().connection().commit(); 但是flush()不行。

	hibernateTemplate.update(salesOrders);
//hibernateTemplate.flush(); //不好用
hibernateTemplate.getSessionFactory().getCurrentSession().connection().commit();//如果没有,前面数据库产生id和下面save冲突
// 保存订单操作日志
salesOperationProcessService.saveSalesOperationProcess(salesOrders,
SalesOrderFlowServiceImpl.toDeliver.getId(),
OrderConstant.LOG_ORDER_MANUAL_ARRANGE_SUBMIT,
Constant.OPERATION_TYPE_POSITIVE, null, null);

放在update后面是为了防止出错回滚,放在update前面就无法回滚

解决hibernate产生的id序列或者setXX不能同步到数据库到问题(this.hibernateTemplate.flush();hibernateTemplate.getSessionFactory().getCurrentSession().connection().commit())的更多相关文章

  1. Hibernate注解映射sequence时出现无序增长问题+hibernate 映射 oracle ID自动增长:

    Hibernate注解映射sequence时出现无序增长问题+hibernate 映射 oracle ID自动增长: 通过Hibernate注解的方式映射oracel数据库的sequence主键生成器 ...

  2. 解决hibernate中的懒加载(延迟加载)问题

    解决hibernate中的懒加载(延迟加载)问题   我们在开发的时候经常会遇到延迟加载问题,在实体映射时,多对一和多对多中,多的一样的属性默认是lazy="true"(即,默认是 ...

  3. hibernate generator class="" id详解

    “assigned”   主键由外部程序负责生成,在   save()   之前指定一个.         “hilo”   通过hi/lo   算法实现的主键生成机制,需要额外的数据库表或字段提供高 ...

  4. 2017/2/8 hibernate + oracle 实现id的自增 同时 hibernate项目跑起来 会自己增加字段的原因 oracle触发器的使用

    hibernate + oracle 实现id的自增 1.在oracle中先创建一个序列 : 序列语法 如下 create  sequence   (序列名称)seq_student_id minva ...

  5. org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Session after a

    1.错误描写叙述 org.hibernate.AssertionFailure: null id in com.you.model.User entry (don't flush the Sessio ...

  6. 报错:org.hibernate.AssertionFailure: null id in com.tt.hibernate.entities.News entry (don't flush the Session after an exception occurs)

    在使用hibernate创建数据库的表格时,出现了如下报错: 十二月 28, 2016 10:17:02 上午 org.hibernate.tool.hbm2ddl.SchemaExport perf ...

  7. 解决hibernate只能插入一条数据的问题

    hibernate初学,根据视频教程写好代码后,发现无论执行多少次main函数,数据库中只有一条数据,尝试多次,后来终于发现问题... 使用的工具是:MYSQL 5.7.13   eclipse 4. ...

  8. 解决Hibernate:could not initialize proxy - no Session(申明:来源于网络)

    解决Hibernate:could not initialize proxy - no Session 地址:http://blog.csdn.net/chenssy/article/details/ ...

  9. org.hibernate.AssertionFailure: null id in xxx.xx.xx的问题

    今日在开发时遇到一个比较奇怪的问题,保存时报这个异常: org.hibernate.AssertionFailure: null id in com.aa.TShoucang null id,这个是什 ...

随机推荐

  1. Little Elephant and Elections CodeForces - 258B

    Little Elephant and Elections CodeForces - 258B 题意:给出m,在1-m中先找出一个数x,再在剩下数中找出6个不同的数y1,...,y6,使得y1到y6中 ...

  2. vue项目node升级后,node-saas报错解决办法

    ERROR in ./node_modules/_extract-text-webpack-plugin@3.0.2@extract-text-webpack-plugin/dist/loader.j ...

  3. AWK整理

    处理模式: awk的处理文本和数据的方式是这样的,它逐行扫描文件,从第一行到最后一行,寻找匹配的特定模式的行,并在这些行上进行你想要的操作.如果没有指定处理动作,则把匹配的行显示到标准输出(屏幕),如 ...

  4. FFmpegUtil

    这几天没事研究音频玩 比如 X配音app的配音功能 录一段 pcm或者wav格式的文件 替换mp4指定位置的音频刚开始卡在 pcm混合以及pcm指定位置插入.思路 一段段的视频进行切割 用到MP4Co ...

  5. Android小玩意儿-- 从头开发一个正经的MusicPlayer(二)

    1·在Service中实例化MusicPlayer,实现对整个播放过程的控制 上一次做到了找到音乐数据,并封装成对象装在ArrayList里,把数据的信息显示在UI上.下面一个阶段就要开始真正的音乐播 ...

  6. Java进化的尽头

    转载需声明:原文链接网址:http://www.artima.com/weblogs/viewpost.jsp?thread=221903 Java: Evolutionary Dead End 我在 ...

  7. java5增加对https的支持

    jdk1.5不支持http协议,jdk1.8默认支持,比较好的解决方案是升级jdk,但是升级jdk风险极大.不能升级jdk的情况下,可以使用如下方式. 利用httpclient,进行封装,从而实现对h ...

  8. vue同胞组件通讯解决方案(以下为一种另外可用vuex解决)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. LibreOJ #101. 最大流

    题目描述 这是一道模板题. 给定 n nn 个点,m mm 条边,给定每条边的容量,求从点 s ss 到点 t tt 的最大流. 输入格式 第一行四个整数 n nn.m mm.s ss.t tt.接下 ...

  10. Three.js模型隐藏或显示

    材质属性.visible查看Three.js文档的基类Material,可以知道材质属性.visible的作用就是控制绑定该材质的模型对象是否可见,默认值是true,LineBasicMaterial ...