2022年7月1日 变量问题
问题描述:
保存数据时 事务提交时报错
问题原因:
两个不同变量指向了统一数据地址
其中一个变量进行了id为null的操作,使得不同两个变量名的对象值同时发生了改变
jpa save方法进行修改时 ,使用了其中一个变量 使得数据不满足条件 所以发生错误
解决办法:
拷贝一个新的变量 进行id为null的操作
techImplPlanFilling viewEntityOld = this.view(entity);
viewEntityOld.setWorkorderStatus("0");
techImplPlanFillingRepository.save(viewEntityOld);
techImplPlanFilling viewentity = new techImplPlanFilling();
org.springframework.beans.BeanUtils.copyProperties(viewEntityOld, viewentity);
viewentity.setId(null);
viewentity.setWorkorderStatus("2");
viewentity.setProcessInstanceId(null);
techImplPlanFilling result = techImplPlanFillingRepository.save(viewentity);
还有可能为使用jpa的注解逐渐 没经过拷贝实体的话未生成注解实体类的id的规则所以报错
真正保存失败的真正原因:
@Id
@GeneratedValue(generator = "snowFlakeId")
@GenericGenerator(name = "snowFlakeId", strategy = "com.cnbmtech.sys.util.id.SnowflakeId")
@ApiModelProperty()
private Long id;
---------------------------------------------------
@Target({ElementType.PACKAGE, ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME) 由jvm加载
public @interface GenericGenerator {
String name();
String strategy();
Parameter[] parameters() default {};
}
由于jpa生成雪花算法id 是由 @GenericGenerator 注解实现
但是@GenericGenerator 注解设置了范围
为由jvm加载 所以保存时 没有生成相应的主键 以至于 jpa save()爆错

浙公网安备 33010602011771号