首页 > 解决方案 > TransientPropertyValueException: object references an unsaved transient instance - 在刷新之前保存瞬态实例:

问题描述

@Entity
@Table(name="tbl_name")
public class Test extends BaseEntity {

    @OneToOne( fetch = FetchType.LAZY)
    @JoinColumn(name = "ALL_ORG_MST_SECTION_ID",nullable = true)
    private AllOrgMst allOrgMstSection;

    @OneToOne(fetch = FetchType.LAZY )
    @JoinColumn(name = "ALL_ORG_MST_SUB_SECTION_ID",nullable = true)
    private AllOrgMst allOrgMstSubSection;


    
}

这是模型。当我插入此模型时,一个对象为空值,然后显示错误

邮递员对象通行证

{
   "allOrgMstSection": {
        "id": 23
    },
    "allOrgMstSubSection": {
        "id": null
    }
}
  @Autowired
    private TestRepository testRepository;

    @Override
    public Test save2(Test entity) {
        return testRepository.save(entity);
    }

在 baseEntity 中有 Id

标签: javaspring-boot

解决方案


只是简单地解决了这个问题

{
   "allOrgMstSection": {
        "id": 23
    },
    "allOrgMstSubSection": null,
}

推荐阅读