I'm starting a Web application, and I have the following structure:
+--------------------------+
| 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;
// ...
}
I'm sending a POST request to one controller with the following JSON:
{
"attribute1": "value 1",
"attribute2": "value 2",
"companyProfile": {
"attribute3": "value 3",
"attribute4": "value 4",
"attribute5": "value 5"
}
}
So now I want to execute the command .save()
and have the application to insert the company in its respectively table and after that, insert the companyProfile
in its respective table, all at once. But that's not happening and I'm getting this: Error Code: 1364. Field 'id_company' doesn't have a default value
.
What may I have been doing wrong?
For your persistence, I think you will have to persist in the order below,
Persist Company object, then
Set the Company object into CompanyProfile and
Finally persist CompanyProfile
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.