循环依赖双向@OneToMany JPA关系 [英] Circular Dependency bidirectional @OneToMany JPA relationship
本文介绍了循环依赖双向@OneToMany 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 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
}
和
@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
}
当我插入带有虚拟Person
的虚拟Goal
后调用goalRepository.findAll()
时,我得到了永无止境的递归循环.
在Person
的goal
列表中添加@JsonIgnore
并没有帮助.我也尝试过@JsonIgnoreProperties
.我想念什么?
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?
推荐答案
如果 jackson 引发了异常,请尝试使用以下两个注释:
In case the exception is raised by jackson, try to use these two annotations:
- @JsonManagedReference
- @JsonBackReference
请参见 http://www.baeldung.com/jackson-双向关系和无限递归
这篇关于循环依赖双向@OneToMany JPA关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文