I have table A and table B, table B have a fk that rererences to table A.
class EntityA
JavaScript
x
@Entity
@Table(name = "tableA")
public class EntityA {
@OneToMany(mappedBy="entityA")
private Set<EntityB> entityBList;
}
class EntityB
JavaScript
@Entity
@Table(name = "tableB")
public class EntityB{
@ManyToOne
@JoinColumn(name="id_entityA", nullable=false)
private EntityA entityA;
}
But when i try to call findAll method from repository (from EntityA) i get:
Could not write JSON: Infinite recursion
Advertisement
Answer
I’ve solved the problem using unidirectional relationship. Used this on my EntityA and erase the property entityA on EntityB.
JavaScript
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "id_nfs")
private Set<EntityB> entityBList
The @JsonManagedReference and @JsonBackReference annotations didnt fix my problem, probably i used wrong.