使用映射ManyToOne OneToMany时,返回流()。collect()不起作用

时间:2018-01-11 12:48:18

标签: java spring spring-boot jackson

Empresa与

I need to either create a custom filter or choose the first approach that I depicted.

佩索阿

@Entity
@Getter
@Setter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
public class Empresa {

    @Id
    @GeneratedValue
    private Long codigo;
    private String razaosocial;
    private String cnpj;
    @OneToMany(mappedBy = "empresa")
    private Set<Pessoa> pessoas;


}

返回我的数据的方法

@Entity
@Getter
@Setter
@ToString
@EqualsAndHashCode
public class Pessoa {

    @Id
    @GeneratedValue
    private Long codigo;
    private String nome;
    private String sexo;
    private String tipoPessoa;
    @ManyToOne
    @JoinColumn(name="codigoempresa")
    private Empresa empresa;
    private String cpf;

}

在创建OneToMany和ManyToOne映射之前,它已经工作了。我完全恢复了我的json。  现在我收到错误。

堆栈跟踪:

  

在   com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149)   〜[jackson-databind-2.8.10.jar:2.8.10] at   com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112)   〜[jackson-databind-2.8.10.jar:2.8.10] at   com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)   〜[杰克逊 - 数据绑定-2.8.10.jar:2.8.10]

这个堆栈跟踪没什么用,但只是在我的控制台eclipse中显示我。

修改 我及时得到了控制台的打印结果e找到了这个: StackOverflow错误: erro

1 个答案:

答案 0 :(得分:2)

您正在获取StackOverflowError。这可能发生的原因之一是无限递归。这正是Jackson试图将您的类反序列化为JSON时发生的情况。您可以使用@JsonIgnoreProperties

解决此问题
@Entity
@Getter
@Setter
@ToString
@EqualsAndHashCode
@NoArgsConstructor
public class Empresa {

    @Id
    @GeneratedValue
    private Long codigo;
    private String razaosocial;
    private String cnpj;
    @OneToMany(mappedBy = "empresa")
    @JsonIgnoreProperties("empresa")
    private Set<Pessoa> pessoas;
}    

@Entity
@Getter
@Setter
@ToString
@EqualsAndHashCode
public class Pessoa {

    @Id
    @GeneratedValue
    private Long codigo;
    private String nome;
    private String sexo;
    private String tipoPessoa;
    @ManyToOne
    @JoinColumn(name="codigoempresa")
    @JsonIgnoreProperties("pessoas")
    private Empresa empresa;
    private String cpf;

}