在Spring Data Rest中保持OneToMany关系

时间:2017-04-11 08:34:20

标签: java json spring hibernate jpa

我有一个名为jobprofile的类,它包含以下OneToMany Relation:

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "profile")
private List<JobLanguageProficiency> jobLanguageProficiency;

引用的课程&#34; JobLanguageProficiency&#34;看起来像这样:

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;
    }
}

基本上,它只包含对语言的两个引用,以及对它的熟练程度。

当我尝试创建/持久化jobprofile时,它会在尝试在数据库中插入语言数据时失败。

这是错误:

  

无法写入HTTP消息:org.springframework.http.converter.HttpMessageNotWritableException:

     

无法写内容:(是java.lang.NullPointerException)(通过   参考链:   org.springframework.data.rest.webmvc.json [&#34;内容&#34]。);嵌套   异常是com.fasterxml.jackson.databind.JsonMappingException :(是   java.lang.NullPointerException)(通过引用链:   org.springframework.data.rest.webmvc.json [&#34;内容&#34]。)

我怀疑错误的来源在于JSON,因为当我手动插入数据时,OneToMany Relation完美地运行。

JSON的相关部分如下所示:

job_language_proficiency: Array[1]
    0: Object
        languages: "http://localhost:9000/api/languages/de"
        proficiency_code: "http://localhost:9000/api/langproficiency/C1"

为什么这不起作用的任何想法?

编辑:更新了JSON,使其包含对象而不是链接。 enter image description here

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; }
}

1 个答案:

答案 0 :(得分:0)

您的JSON结构应与您的Java Mapping相匹配,否则您的ORM将不会保留您的对象,这就是您获得此类映射错误的原因。

在您的实体中,您与JSON中应该是object的两个实体有关系:

@OneToOne
private Languages languages;

@OneToOne
private LangProficiency langProficiency;

但是在你的JSON中,你已经将这两个对象设置为简单的字符串:

languages: "http://localhost:9000/api/languages/de"
proficiency_code: "http://localhost:9000/api/langproficiency/C1"

它们应匹配LanguageslangProficiency个对象,而proficiency_code应分别命名为langProficiency,就像您的Java属性一样。