【问题标题】:OneToMany relationship causing infinite loop using JPA使用 JPA 导致无限循环的 OneToMany 关系
【发布时间】:2021-01-26 23:29:27
【问题描述】:

在发布此问题之前,我进行了一些搜索,并尝试在 ManyToOne 端和 OneToMany 端添加@JsonIgnore,但没有任何效果。我正在定义两个类及其关系如下:

Cidade类

@Entity
public class Cidade implements Serializable {
    
    private static final long serialVersionUID = 8653970141174181222L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String nome;
    private String estado;
    @OneToMany(mappedBy = "cidade", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<Cliente> clientes = new ArrayList<Cliente>();
    
    public Cidade() {
        
    }
    
    public Cidade(String nome, String estado) {
        this.nome = nome;
        this.estado = estado;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }
    
    public String getEstado() {
        return estado;
    }

    public void setEstado(String estado) {
        this.estado = estado;
    }

    public List<Cliente> getClientes() {
        return clientes;
    }

    public void setClientes(List<Cliente> clientes) {
        this.clientes = clientes;
    }                   
}

和客户类:

@Entity
public class Cliente implements Serializable{

    private static final long serialVersionUID = -9148095834550183273L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    private String nome;
    private String sexo;
    private String dataNascimento;
    private Integer idade;
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "ID_CIDADE", nullable = false)
    private Cidade cidade;
    
    
    public Cliente() {
        
    }
    
    public Cliente(String nome, String sexo, String dataNascimento, Integer idade, Cidade cidade) {
        this.nome = nome;
        this.sexo = sexo;
        this.dataNascimento = dataNascimento;
        this.idade = idade;
        this.cidade = cidade;
    }

    public Cliente(Integer id, String nome) {
        this.id = id;
        this.nome = nome;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getSexo() {
        return sexo;
    }

    public void setSexo(String sexo) {
        this.sexo = sexo;
    }

    public String getDataNascimento() {
        return dataNascimento;
    }

    public void setDataNascimento(String dataNascimento) {
        this.dataNascimento = dataNascimento;
    }

    public Integer getIdade() {
        return idade;
    }

    public void setIdade(Integer idade) {
        this.idade = idade;
    }

    public Cidade getCidade() {
        return cidade;
    }

    public void setCidade(Cidade cidade) {
        this.cidade = cidade;
    }
    
    
}

一切都按预期工作,但是当我尝试使用 rest api 获取一个 Cliente 时,它​​会导致无限循环。我该如何解决这个问题?

【问题讨论】:

    标签: java jpa spring-data-jpa


    【解决方案1】:

    我建议您在 @OneToMany 字段上使用 @JsonBackReference 并在 @ManyToOne 字段上使用 @JsonManagedReference。请参阅下面的示例代码:

    Cicade 课

    @OneToMany(mappedBy = "cidade", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonBackReference
    private List<Cliente> clientes = new ArrayList<Cliente>();
    

    客户类

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "ID_CIDADE", nullable = false)
    @JsonManagedReference
    private Cidade cidade;
    

    【讨论】:

    • 它给了我这个错误:No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor
    【解决方案2】:

    要解决这个问题,你必须写

    @JsonBackReference(value = "cidade-cliente")  // ----------------> Unique value
    private List<Cliente> clientes = new ArrayList<Cliente>();
    

    在 Cicade 类中,

    @JsonManagedReference(value = "cidade-cliente")  // ----------------> Unique value same as that of the Cliente list of Cidade class
    private Cidade cidade;
    

    在客户端类中。

    另外,请查看JPA: Having lists on both ends without infinite loop

    【讨论】:

      猜你喜欢
      • 2016-01-18
      • 2023-03-09
      • 2017-01-09
      • 2018-11-29
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多