[英]java.sql.SQLException: 'Field 'voc_id' doesn't have a default value'. using Spring JPA
I'm trying to insert Voc, Recovery class in One to one relationship.我正在尝试在一对一关系中插入 Voc, Recovery class。
Voc is gonna be inserted first and it's pk will get into Recovery class as it's foreign key. Voc 将首先插入,它的 pk 将进入 Recovery class,因为它是外键。 (Which is gonna be used like PK of Recovery class)
(这将被用作恢复类的PK)
@Data @AllArgsConstructor @NoArgsConstructor @Entity @Table(name="voc") public class Voc { @Id @Column(name="id") @GeneratedValue(strategy=GenerationType.IDENTITY) int id; @OneToOne(mappedBy="voc", cascade=CascadeType.ALL) Recovery recovery; // some other variables... }
@Data @Entity @Table(name="recovery") public class Recovery { @Id @Column(name="voc_id") @GeneratedValue(strategy=GenerationType.IDENTITY) int vocId; @OneToOne @PrimaryKeyJoinColumn(name="voc_id") Voc voc; // some other variables... }
I'm trying to do the way of No.3 from this .我正在尝试从这个开始做No.3的方式。
But when I call the save() method of JpaRepository, it throws error message ' Field 'voc_id' doesn't have a default value '.但是当我调用 JpaRepository 的 save() 方法时,它会抛出错误消息“ Field 'voc_id' doesn't have a default value ”。
What am I missing?我错过了什么?
Or is there better way than this to make what I want?或者有比这更好的方法来制作我想要的东西吗?
Error states that 'voc_id' doesn't have a default value that is while creating table there is no default value assigned by you in case voc_id is NOT NULL. So, if in code there is null value getting assigned to voc_id it will throw exception.错误指出“voc_id”没有默认值,即在创建表时没有您分配的默认值,以防 voc_id 不是 NULL。因此,如果在代码中有 null 值分配给 voc_id,它将抛出异常. Best way to avoid this error is to define some default value to all NOT NULL attributes in create table query.
避免此错误的最佳方法是在创建表查询中为所有 NOT NULL 属性定义一些默认值。 In your case you also need to check why it is getting null value since it is auto generated.
在您的情况下,您还需要检查为什么它会获得 null 值,因为它是自动生成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.