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()爆错

posted @   cai鸟一号  阅读(26)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示