问题
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