为了账号安全,请及时绑定邮箱和手机立即绑定

如何正确发布到双向关系

如何正确发布到双向关系

慕田峪9158850 2022-09-07 17:34:26
因此,我使用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;@Entitypublic 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;    }}
查看完整描述

2 回答

?
隔江千里

TA贡献1906条经验 获得超10个赞

此问题与 JPA 双向映射无关。它在反序列化时引发错误。
Partida -> 应具有零参数构造函数

请求有效负载应具有 { “partida”:{“id”:123}},以便填充 partida 对象属性。


查看完整回答
反对 回复 2022-09-07
?
MMMHUHU

TA贡献1834条经验 获得超8个赞

有关详细信息,请查看此链接杰克逊图书馆。


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

    public  Partida(){

    //Default constructor required here 

    }

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

    }


}

如果问题未解决,请尝试 JSON 创建者


@JsonCreator

    public Partida(@JsonProperty("id") Long id, @JsonProperty("timeA") String 

    timeA, @JsonProperty("timeB") String timeB, @JsonProperty("desc") boolean

    visitante) {

    this.id = id;

    this.timeA = timeA;

    this.timeB= timeB;

    this.visitante= visitante;


查看完整回答
反对 回复 2022-09-07
  • 2 回答
  • 0 关注
  • 77 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信