【发布时间】: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 表结构
全包表
【问题讨论】:
-
能否请您展示一下您是如何插入价值的。
-
@SternK :我更新了问题并添加了邮递员电话,请参考。谢谢!
标签: mysql spring-boot hibernate spring-data-jpa