Java 循环依赖双向@OneToMany JPA关系

Java 循环依赖双向@OneToMany JPA关系,java,hibernate,spring-boot,jpa,Java,Hibernate,Spring Boot,Jpa,鉴于以下两个实体: @Entity public class Goal { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String description; private BigDecimal amount; @Email private String email; @Email private String supervisorEmail; private Loc

鉴于以下两个实体:

@Entity
public class Goal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String description;

private BigDecimal amount;

@Email
private String email;

@Email
private String supervisorEmail;

private LocalDateTime deadline;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private PaymentPurpose purpose;

@Enumerated(EnumType.STRING)
private GoalStatus status;

@ManyToOne(cascade = CascadeType.ALL)
private Person person;



//getters and setters
}

@实体
公共阶层人士{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
私有字符串名;
私有字符串lastName;
@电子邮件
私人字符串电子邮件;
@OneToMany(mappedBy=“person”)
私人清单目标;
//接球手和接球手
}
当我在插入一个虚拟的
Goal
和一个虚拟的
Person
后调用
goalRepository.findAll()
时,我得到一个永无止境的递归循环。
@JsonIgnore
添加到
Person
中的
goal
列表中没有帮助。我还尝试了
@JsonIgnoreProperties
。我遗漏了什么?

如果jackson提出了异常,请尝试使用以下两种注释:

  • @JsonManagedReference
  • @JsonBackReference

@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String firstName;
    private String lastName;

    @Email
    private String email;

    @OneToMany(mappedBy = "person")
    private List<Goal> goals;

    //getters and setters

}