Circular Dependency bidirectional @OneToMany JPA relationship

别说谁变了你拦得住时间么 提交于 2021-01-29 12:39:10

问题


Given the following two entities:

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

and

@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

}

When I call goalRepository.findAll() after inserting a Dummy Goal with a Dummy Person, I get a never ending recursive loop. Adding @JsonIgnore to the goal list in Person didn't help. I also tried @JsonIgnoreProperties. What am I missing?


回答1:


In case the exception is raised by jackson, try to use these two annotations:

  • @JsonManagedReference
  • @JsonBackReference

see http://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion



来源:https://stackoverflow.com/questions/50947448/circular-dependency-bidirectional-onetomany-jpa-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!