首页 > 数据库技术 > 详细

解决hibernate产生的id序列或者setXX不能同步到数据库到问题(this.hibernateTemplate.flush();)

时间:2014-02-18 11:13:17      阅读:543      评论:0      收藏:0      [点我收藏+]

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

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

-->hibernate的实体都是存储在缓存中的,
   所以你会发现有的时候当你创建出两个主键相通的实体的时候会报错。
   正常情况是当你调用save方法的时候,这个实体对象未必已经保存到数据库了,
   调用close方法的时候,对象才真正保存如数据库。当你调用flush方法的时候是强制将对象保存到数据库。
1
2
3
4
5
6
7
8
9
10
11
12
13
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产生的id序列或者setXX不能同步到数据库到问题(this.hibernateTemplate.flush();)

原文:http://www.cnblogs.com/kyxyes/p/3553295.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!