我是靠谱客的博主 哭泣金针菇,这篇文章主要介绍JPA save方法及联更新的BUG,现在分享给大家,希望可以做个参考。

  Jpa在save对象的时候,及联更新的当前对象外键关联的子对象,很奇怪,insertable和updatable的配置未生效,如下:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "business_id", referencedColumnName = "serial_no", insertable = false, updatable = false)
@NotFound(action = NotFoundAction.IGNORE)
private OrderInfo orderInfo;

保存新的对象代码如下

BaseInfoRespDTO respDTO = new BaseInfoRespDTO();
OrderInfoVo queryOrder = new OrderInfoVo();
queryOrder.setSerialNo(orderOperationVo.getOrderNo());
List<OrderInfo> all = orderInfoService.findAll(queryOrder); // 1、这一步存在一个aop,脱敏相关数据
Assert.isFalse(all.isEmpty(), "订单编号不存在");
OrderOperation orderOperation = new OrderOperation();
BeanCpUtils.copyPropertiesIgnoreNull(orderOperationVo, orderOperation);
String cityCode = dynamicConstantService.getCityCode();
//售后订单服务号:险种编码+地区编码+7位数字
orderOperation.setId(Constant.COVERAGE_NUMBER + cityCode + new Random().nextInt(9999999));
orderOperation.setProcessType(ProcessType.valueOf(orderOperationVo.getOrderProcessType()));
orderOperation.setStatus(OrderOperationStatus.valueOf(orderOperationVo.getOperStatus()));
orderOperation.setCreatedBy(createBy);
orderOperation.setCreatedById(createById);
orderOperation.setCreatedTime(new Date());
orderOperation.setCityCode(cityCode);
orderOperation.setUpdateDate(new Date());
orderOperation.setBusinessId(orderOperationVo.getOrderNo());
orderOperation.setDelete(false);
OrderOperation saveData = orderOperationService.save(orderOperation);

在标记1的地方用jpa查询的数据,进行了非空判断,是经过了aop的查询,在下面保存的时候,及联更新OrderInfo表,以及下面的关联表,很奇怪insertable = false, updatable = false没有生效。

解决办法:

新增了一个方法,去掉了走aop的查询,结果不误更新了,但是其实是库里的原来的数据更新了一遍。

最后

以上就是哭泣金针菇最近收集整理的关于JPA save方法及联更新的BUG的全部内容,更多相关JPA内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(151)

评论列表共有 0 条评论

立即
投稿
返回
顶部