應堅持一對多的關係我有一個叫jobprofile類,它包含以下一對多關係:在Spring數據休息
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "profile")
private List<JobLanguageProficiency> jobLanguageProficiency;
引用的類「JobLanguageProficiency」看起來是這樣的:
package ch.alv.jobmatch.entity.aux;
import ch.alv.jobmatch.entity.job.Jobprofile;
import javax.persistence.*;
@Entity
@Table(name = "job_language_proficiency")
public class JobLanguageProficiency {
@Id
private int id;
@ManyToOne
@JoinColumn(name = "jobprofile_jobprofile_id")
private Jobprofile profile;
@OneToOne
private Languages languages;
@OneToOne
private LangProficiency langProficiency;
public Languages getLanguages() {
return languages;
}
public void setLanguages(Languages languages) {
this.languages = languages;
}
public LangProficiency getLangProficiency() {
return langProficiency;
}
public void setLangProficiency(LangProficiency langProficiency) {
this.langProficiency = langProficiency;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Jobprofile getProfile() {
return profile;
}
public void setProfile(Jobprofile profile) {
this.profile = profile;
}
}
基本上,它只包含兩種對一種語言的引用,以及對它的熟練程度。
當我嘗試創建/保留作業配置文件時,只要嘗試將語言數據插入到數據庫中,就會失敗。
這是錯誤:
Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException:
Could not write content: (was java.lang.NullPointerException) (through reference chain: org.springframework.data.rest.webmvc.json.["content"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: org.springframework.data.rest.webmvc.json.["content"])
我懷疑錯誤的根源在於JSON,因爲一對多關係完美的作品,當我手動插入數據。
的JSON的相關部分看起來是這樣的:
job_language_proficiency: Array[1]
0: Object
languages: "http://localhost:9000/api/languages/de"
proficiency_code: "http://localhost:9000/api/langproficiency/C1"
任何想法,爲什麼這不起作用?
LangProficiency實體:
package ch.alv.jobmatch.entity.aux;
import javax.persistence.*;
@Entity
@Table(name = "LangProficiency")
public class LangProficiency {
@Id
@Column(name = "proficiency_code")
private String proficiency_code;
private String description_de;
private String description_fr;
private String description_it;
private String description_en;
public String getProficiency_code() {
return proficiency_code;
}
public void setProficiency_code(String proficiency_code) {
this.proficiency_code = proficiency_code;
}
public String getDescription_de() {
return description_de;
}
public void setDescription_de(String description_de) {
this.description_de = description_de;
}
public String getDescription_fr() {
return description_fr;
}
public void setDescription_fr(String description_fr) {
this.description_fr = description_fr;
}
public String getDescription_it() {
return description_it;
}
public void setDescription_it(String description_it) {
this.description_it = description_it;
}
public String getDescription_en() { return description_en; }
public void setDescription_en(String description_en) { this.description_en = description_en; }
}
語言實體:
package ch.alv.jobmatch.entity.aux;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "Languages")
public class Languages implements Serializable {
@Id
@Column(name = "languages")
private String languages;
private String description_de;
private String description_fr;
private String description_it;
private String description_en;
public String getLanguages() {
return languages;
}
public void setLanguages(String languages) {
this.languages = languages;
}
public String getDescription_de() {
return description_de;
}
public void setDescription_de(String description_de) {
this.description_de = description_de;
}
public String getDescription_fr() {
return description_fr;
}
public void setDescription_fr(String description_fr) {
this.description_fr = description_fr;
}
public String getDescription_it() {
return description_it;
}
public void setDescription_it(String description_it) {
this.description_it = description_it;
}
public String getDescription_en() { return description_en; }
public void setDescription_en(String description_en) { this.description_en = description_en; }
}
語言似乎是在您的映射中的對象,但它顯示爲您的JSON中的字符串。 – TheBakker
您是否嘗試將兩個實體的ID更改爲Long。假設你需要一個'long'類型的id來支持Spring數據。 [鏈接](https://examples.javacodegeeks.com/enterprise-java/spring/spring-data-rest-example/) –
@TheBakker據我所知,使用鏈接也應該工作。在JSON中實現完整的對象對於這種特定的情況是非常不切實際的。 –