【问题标题】:One to One mapping get null value Spring boot JPA Hibernate一对一映射获取空值 Spring boot JPA Hibernate
【发布时间】:2020-12-17 00:54:23
【问题描述】:

我已经实现了一对一的关系,当我尝试插入值时,它给出了以下异常。

我已经尝试了许多 stackoverflow 答案,但还没有运气。我不知道出了什么问题。

例外:

org.springframework.dao.DataIntegrityViolationException: not-null 属性引用空值或瞬态值

非空属性引用空值或瞬态值

FullPack 实体

@Entity(name = "FullPack")
@Table(name = "fullpack")
@TypeDefs({
        @TypeDef(name = "json", typeClass = JsonStringType.class),
        @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})
public class FullPack {

    @Id
    @Column(name = "fullpack_id")
    int fullPackId;

    @Type(type = "json")
    @Column(columnDefinition = "json")
    private String sequence;

    @Type(type = "json")
    @Column(columnDefinition = "json")
    private String channelRestriction;


    @Type(type = "json")
    @Column(columnDefinition = "json")
    private String actNotAllowed;


    @Type(type = "json")
    @Column(columnDefinition = "json")
    private String packCompose;

}

打包序列实体

@Entity
public class PackSequence {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int packSequenceId;

    int sequenceId;

    int packId;


    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "fullpack_packsequence_id", referencedColumnName = "fullpack_id", nullable = false)
    private FullPack fullPack;
}

向 FullPack 表插入记录(有意为 JSON 字段插入空值)

{
    "fullPackId": 1,
    "sequence": null,
    "channelRestriction": null,
    "actNotAllowed": null,
    "packCompose": null
}

在 PackSequence 表中插入记录

根据 Chun 的回答更新,但存在同样的问题

  {
        "sequenceId":1,
        "packId":2,
        "fullpack" : {
            "fullpack_id":1
        }
  }

Mysql 表结构

全包表

PackSequence 表

【问题讨论】:

  • 能否请您展示一下您是如何插入价值的。
  • @SternK :我更新了问题并添加了邮递员电话,请参考。谢谢!

标签: mysql spring-boot hibernate spring-data-jpa


【解决方案1】:

我认为这可能与 Postman 到 PackSequence 和 FullPack 对象的 Json 转换有关

您将需要使用 JsonProperty 来帮助重新映射字段名称。

我必须修改您的 PackSequence 和 FullPack 对象如下


@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class PackSequence {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    int packSequenceId;

    int sequenceId;

    int packId;


    @OneToOne()
    @JoinColumn(name = "fullpack_packsequence_id", referencedColumnName = "fullpack_id", nullable = false)
    @JsonProperty("fullpack")
    private FullPack fullPack;

    // omit getter and setter
}

@Entity(name = "FullPack")
@Table(name = "fullpack")
@JsonIgnoreProperties(ignoreUnknown = true)
public class FullPack
{

    @Id
    @Column(name = "fullpack_id")
    @JsonProperty("fullpack_id")
    int fullPackId;

    private String sequence;
    private String channelRestriction;
    private String actNotAllowed;
    private String packCompose;

    // omit getter and setter
}

假设您通过 RestController 等从 JSON Payload 插入对象。

PackSequence json 对象应如下所示

{
    "sequenceId":1,
    "packId":2,
    "fullpack" : {
        "fullpack_id":1
    }
}

【讨论】:

  • 同样的问题 :( 运气不好
  • 这是发送 Postman JSON 请求的正确方法,我会根据我的问题对此进行更新。但我仍然遇到同样的异常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 2021-05-26
  • 1970-01-01
  • 1970-01-01
  • 2019-11-08
  • 2020-11-17
  • 1970-01-01
相关资源
最近更新 更多