我在兩個班級之間有一對一的關系。我想創建階段專案而不必插入候選人 ID,因為我之后得到了候選人,所以他們基本上不存在。
現在我收到錯誤:
無法執行陳述句;SQL [不適用];約束[空];
因為我沒有發送candidateId。
這是第一堂課:
public class PhaseItems {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "phaseI_id")
private long id;
private String PhaseItem;
@ManyToMany(fetch = FetchType.LAZY,
mappedBy = "items")
@JsonIgnore
private List<PhaseTemplate> template = new ArrayList<>();
@OneToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "candidate_id", nullable = true)
private Candidate candidate;
public PhaseItems() {
super();
}
public PhaseItems(long id, String phaseItem) {
super();
this.id = id;
PhaseItem = phaseItem;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getPhaseItem() {
return PhaseItem;
}
public void setPhaseItem(String phaseItem) {
PhaseItem = phaseItem;
}
public List<PhaseTemplate> getTemplate() {
return template;
}
public void setTemplate(List<PhaseTemplate> template) {
this.template = template;
}
public Candidate getCandidate() {
return candidate;
}
public void setCandidate(Candidate candidate) {
this.candidate = candidate;
}
@Override
public String toString() {
return "PhaseItems [id=" id ", PhaseItem=" PhaseItem ", template=" template "]";
}
這是第二類:
@Entity
@Table(name= "candidats")
public class Candidate {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "candidate_id")
private long id;
private String fullname;
private String username;
@Column(nullable = false, unique = true, length = 45)
private String email;
private String adress;
private String phoneNumber;
private String password;
@Column(name = "status")
private String status;
@OneToOne(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "candidate")
private PhaseItems phases;
public Candidate(){
}
public Candidate(String fullname,String username,String email, String adress, String phoneNumber, String password,
List<JobApplication> appliedJobs) {
super();
this.fullname = fullname;
this.username = username;
this.email = email;
this.adress = adress;
this.phoneNumber = phoneNumber;
this.password = password;
this.appliedJobs = appliedJobs;
}
public Candidate(String fullname,String username, String email, String adress, String phoneNumber, String password) {
super();
this.fullname = fullname;
this.username = username;
this.email = email;
this.adress = adress;
this.phoneNumber = phoneNumber;
this.password = password;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAdress() {
return adress;
}
public void setAdress(String adress) {
this.adress = adress;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<JobApplication> getAppliedJobs() {
return appliedJobs;
}
public void setAppliedJobs(List<JobApplication> appliedJobs) {
this.appliedJobs = appliedJobs;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
uj5u.com熱心網友回復:
在資料庫中將列設定candidate_id為可為空。注釋的 Java 屬性被nullable = true忽略。@JoinColumn
uj5u.com熱心網友回復:
您的表 phase_items 中的列 ?candidate_id ? 在您的架構中是否可以為空?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/487032.html
上一篇:潛在空外鍵的JPA多對一映射
