JPA保存有时生效,有时无效

问题描述:由于项目需要需要定时从其他服务接口查询数据,把符合条件的数据插入预警表,结果发现插入的数据总时插入数据不全或者没插入数据。

原始代码:

public class TestServiceImpl  {

    @Autowired
    private TestServiceImpl testServiceService;
    @Autowired
    private TestDao testDao;

    /**
     *数据保存测试
     *
     * @param testEntity
     */

   @Transactional(rollbackFor = Exception.class)
    public void saveEntity(TestEntity testEntity) {
       String vin = testEntity.getVin();
       Date createDate = testEntity.getCreateDate();
       if (null != createDate) {
           String createDateStr = DateUtil.format(createDate);
           TestEntity originDrivingWarning = testDao.findByVinCAndCreateDate(vin, createDateStr);
           if (null != originDrivingWarning) {
               testEntity.setSid(originDrivingWarning.getSid());
               testEntity.setVersion(originDrivingWarning.getVersion()+ Constants.DEFAULT_ONE);
           }
       }
       testDao.save(testEntity);
    }


    
    /**
     * 校验预警,保存记录
     */

    public void checkWarning() {
        PageDto<TestWarning> pageDto = testDao.findWarningList();
        // 处理数据
        List<TestWarning> rows = pageDto.getRows();
  
        rows.parallelStream().forEach(warningVO->{
            TestEntity  testWarningEntity = new TestEntity();
            testWarningEntity.setVin(warningVO.getVin());
            testWarningEntity.setAddress("上海");
             //保存(问题出现在这)
            testServiceService.saveEntity(testWarningEntity);
        });

    }
}

更改后的代码:

public class TestServiceImpl  {

    @Autowired
    private TestServiceImpl testServiceService;
    @Autowired
    private TestDao testDao;

    /**
     *数据保存测试
     *
     * @param testEntity
     */

   @Transactional(rollbackFor = Exception.class)
    public void saveEntity(TestEntity testEntity) {
       String vin = testEntity.getVin();
       Date createDate = testEntity.getCreateDate();
       if (null != createDate) {
           String createDateStr = DateUtil.format(createDate);
           TestEntity originDrivingWarning = testDao.findByVinCAndCreateDate(vin, createDateStr);
           if (null != originDrivingWarning) {
               testEntity.setSid(originDrivingWarning.getSid());
               testEntity.setVersion(originDrivingWarning.getVersion()+ Constants.DEFAULT_ONE);
           }
       }
       testDao.save(testEntity);
    }


    /**
     * 批量保存数据
     * @param testEntityList
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchSaveEntity(List<TestEntity>
                                            testEntityList) {
        testEntityList.parallelStream().forEach(vo->{
            String vin = vo.getVin();
            Date createDate = vo.getCreateDate();
            if (null != createDate) {
                String createDateStr = DateUtil.format(createDate);
                TestEntity originDrivingWarning = testDao.findByVinCAndCreateDate(vin, createDateStr);
                    if (null != originDrivingWarning) {
                        vo.setSid(originDrivingWarning.getSid());
                        vo.setVersion(originDrivingWarning.getVersion()+ Constants.DEFAULT_ONE);
                     }
                 }
            }
        );

        testDao.save(testEntityList);
    }

    /**
     * 校验预警,保存记录
     */

    public void checkWarning() {
        PageDto<TestWarning> pageDto = testDao.findWarningList();
        // 处理数据
        List<TestWarning> rows = pageDto.getRows();
        List<TestEntity> saveEntityList = new ArrayList<>();
        rows.parallelStream().forEach(warningVO->{
            TestEntity  testWarningEntity = new TestEntity();
            testWarningEntity.setVin(warningVO.getVin());
            testWarningEntity.setAddress("上海");
               //step1:先放在待插入列表
             saveEntityList.add(testWarningEntity);
        });
          
        if(saveEntityList.size()>0){
            //step2:有预警需要保存,则批量保存效率更快。
            testServiceService.batchSaveEntity(saveEntityList);
        }
    }
}

单个插入改成批量插入以同一个事务提交数据。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值