Skip to content
Advertisement

Java JPA SpringBoot

I have table A and table B, table B have a fk that rererences to table A.

class EntityA

@Entity
@Table(name = "tableA")
public class EntityA {


... ... ...

   @OneToMany(mappedBy="entityA")
   private Set<EntityB> entityBList; 
    
}

class EntityB

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

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

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement