【发布时间】:2023-03-30 22:15:01
【问题描述】:
我正在启动一个 Web 应用程序,我有以下结构:
+--------------------------+
| Company |
|--------------------------|
| id PK, NN, AI |
| attibute1 |
| attribute2 |
+--------------------------+
+--------------------------+
| CompanyProfile |
|--------------------------|
| id PK, NN, AI |
| id_company FK, NN |
| attribute3 |
| attribute4 |
| attribute5 |
+--------------------------+
public class Company {
@Id
@GeneratedValue
@Column(unique = true, nullable = false)
private Long id;
private String attribute1;
private String attribute2;
@OneToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "id_company", unique = true)
private CompanyProfile companyProfile;
// ...
}
public class CompanyProfile {
@Id
@GeneratedValue
@Column(unique = true, nullable = false)
private Long id;
private String attribute3;
private String attribute4;
private String attribute5;
// ...
}
我正在使用以下 JSON 向一个控制器发送 POST 请求:
{
"attribute1": "value 1",
"attribute2": "value 2",
"companyProfile": {
"attribute3": "value 3",
"attribute4": "value 4",
"attribute5": "value 5"
}
}
所以现在我想执行命令.save() 并让应用程序将公司插入其各自的表中,然后将companyProfile 插入其各自的表中,一次。但这并没有发生,我得到了这个:Error Code: 1364. Field 'id_company' doesn't have a default value。
我可能做错了什么?
【问题讨论】:
-
您的映射告诉 JPA company 表中有一个 id_company 列,引用 companyprofile 表中的 ID。
-
@JBNizet 是正确的。您需要将该列移到正确的表格中。
-
它也不起作用,你能发一个例子吗?
标签: java spring-boot