如何正确发布到双向关系

因此,我使用springboot,spring-data和Jackson来实施API,但是当我试图将请求发布到具有双向关系@OneToMany的端点时,我遇到了一些麻烦。


我没有那么多背景,所以我需要真正的帮助。


我有两个实体:


帕蒂达


package lucas.duarte.jazz.model.bean;


import java.io.Serializable;

import java.util.List;


import javax.persistence.CascadeType;

import javax.persistence.Entity;

import javax.persistence.FetchType;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.OneToMany;


import com.fasterxml.jackson.annotation.JsonCreator;

import com.fasterxml.jackson.annotation.JsonManagedReference;

import com.fasterxml.jackson.annotation.JsonProperty;


@Entity

public class Partida implements Serializable {

    private static final long serialVersionUID = 1L;


    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;

    private String timeA;

    private String timeB;

    private boolean visitante;

    @OneToMany(mappedBy = "partida", fetch = FetchType.LAZY, cascade = CascadeType.ALL)

    private List<Set> sets;



    public List<Set> getSets() {

        return sets;

    }


    public void setSets(List<Set> sets) {

        this.sets = sets;

    }


    public Long getId() {

        return id;

    }


    public void setId(Long id) {

        this.id = id;

    }


    public String getTimeA() {

        return timeA;

    }


    // Mocado o valor pois o Time A sempre e a Sao Judas

    public void setTimeA(String timeA) {

        this.timeA = timeA;

    }


    public String getTimeB() {

        return timeB;

    }


    public void setTimeB(String timeB) {

        this.timeB = timeB;

    }


    public boolean isVisitante() {

        return visitante;

    }


    public void setVisitante(boolean visitante) {

        this.visitante = visitante;

    }


}


慕田峪9158850
浏览 78回答 2
2回答

隔江千里

此问题与 JPA 双向映射无关。它在反序列化时引发错误。Partida -> 应具有零参数构造函数请求有效负载应具有 { “partida”:{“id”:123}},以便填充 partida 对象属性。

MMMHUHU

有关详细信息,请查看此链接杰克逊图书馆。@Entitypublic class Partida implements Serializable {&nbsp; &nbsp; private static final long serialVersionUID = 1L;&nbsp; &nbsp; @Id&nbsp; &nbsp; @GeneratedValue(strategy = GenerationType.IDENTITY)&nbsp; &nbsp; private Long id;&nbsp; &nbsp; private String timeA;&nbsp; &nbsp; private String timeB;&nbsp; &nbsp; private boolean visitante;&nbsp; &nbsp; public&nbsp; Partida(){&nbsp; &nbsp; //Default constructor required here&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp; @OneToMany(mappedBy = "partida", fetch = FetchType.LAZY, cascade =&nbsp;&nbsp; &nbsp; CascadeType.ALL)&nbsp; &nbsp; private List<Set> sets;&nbsp; &nbsp; public List<Set> getSets() {&nbsp; &nbsp; &nbsp; &nbsp; return sets;&nbsp; &nbsp; }&nbsp; &nbsp; public void setSets(List<Set> sets) {&nbsp; &nbsp; &nbsp; &nbsp; this.sets = sets;&nbsp; &nbsp; }&nbsp; &nbsp; public Long getId() {&nbsp; &nbsp; &nbsp; &nbsp; return id;&nbsp; &nbsp; }&nbsp; &nbsp; public void setId(Long id) {&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; public String getTimeA() {&nbsp; &nbsp; &nbsp; &nbsp; return timeA;&nbsp; &nbsp; }&nbsp; &nbsp; // Mocado o valor pois o Time A sempre e a Sao Judas&nbsp; &nbsp; public void setTimeA(String timeA) {&nbsp; &nbsp; &nbsp; &nbsp; this.timeA = timeA;&nbsp; &nbsp; }&nbsp; &nbsp; public String getTimeB() {&nbsp; &nbsp; &nbsp; &nbsp; return timeB;&nbsp; &nbsp; }&nbsp; &nbsp; public void setTimeB(String timeB) {&nbsp; &nbsp; &nbsp; &nbsp; this.timeB = timeB;&nbsp; &nbsp; }&nbsp; &nbsp; public boolean isVisitante() {&nbsp; &nbsp; &nbsp; &nbsp; return visitante;&nbsp; &nbsp; }&nbsp; &nbsp; public void setVisitante(boolean visitante) {&nbsp; &nbsp; &nbsp; &nbsp; this.visitante = visitante;&nbsp; &nbsp; }}如果问题未解决,请尝试 JSON 创建者@JsonCreator&nbsp; &nbsp; public Partida(@JsonProperty("id") Long id, @JsonProperty("timeA") String&nbsp;&nbsp; &nbsp; timeA, @JsonProperty("timeB") String timeB, @JsonProperty("desc") boolean&nbsp; &nbsp; visitante) {&nbsp; &nbsp; this.id = id;&nbsp; &nbsp; this.timeA = timeA;&nbsp; &nbsp; this.timeB= timeB;&nbsp; &nbsp; this.visitante= visitante;}&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java