简体   繁体   English

在Spring Data Rest中保持OneToMany关系

[英]Persisting OneToMany Relation in Spring Data Rest

I have a class called jobprofile, which contains the following OneToMany Relation: 我有一个名为jobprofile的类,它包含以下OneToMany Relation:

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

The referenced class "JobLanguageProficiency" looks like this: 引用的类“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;
    }
}

Basically, it just contains two references to a language, and a proficiency for it. 基本上,它只包含两种语言的引用,以及它的熟练程度。

When I try to create/persist a jobprofile, it fails as soon as it tries to insert the language data in the database. 当我尝试创建/持久化jobprofile时,它会在尝试在数据库中插入语言数据时失败。

This is the error: 这是错误:

Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: 无法写入HTTP消息:org.springframework.http.converter.HttpMessageNotWritableException:

Could not write content: (was java.lang.NullPointerException) (through reference chain: org.springframework.data.rest.webmvc.json.["content"]); 无法写内容:(是java.lang.NullPointerException)(通过引用链: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"]) 嵌套异常是com.fasterxml.jackson.databind.JsonMappingException :(是java.lang.NullPointerException)(通过引用链:org.springframework.data.rest.webmvc.json。[“content”])

I suspect the source of the error lies in the JSON, since the OneToMany Relation works perfectly when I insert the data manually. 我怀疑错误的来源在于JSON,因为当我手动插入数据时,OneToMany Relation完美地工作。

The relevant part of the JSON looks like this: JSON的相关部分如下所示:

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

Any ideas why this doesn't work? 任何想法为什么这不起作用?

EDIT: Updated the JSON so it contains objects instead of links. 编辑:更新了JSON,因此它包含对象而不是链接。 在此输入图像描述

LangProficiency entity: 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; }
}

Languages entity: 语言实体:

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

Your JSON structure should match your Java Mapping, otherwise your object won't be persisted by your ORM, that's why you are getting such Mapping error. 您的JSON结构应与您的Java Mapping匹配,否则您的ORM将不会保留您的对象,这就是您获得此类映射错误的原因。

In your case in your Entity you have a relationship with two entities that should be object s in your JSON: 在您的实体中,您与两个实体有关系,这两个实体应该是JSON中的object

@OneToOne
private Languages languages;

@OneToOne
private LangProficiency langProficiency;

But in your JSON, you have set those two objects as simple strings: 但是在您的JSON中,您已将这两个对象设置为简单字符串:

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

They should be matching Languages and langProficiency objects, and proficiency_code should be named langProficiency respectively like your Java attribute. 它们应该匹配LanguageslangProficiency对象, proficiency_code应该像您的Java属性一样分别命名为langProficiency

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM