Home > Software engineering >  Java JPA SpringBoot
Java JPA SpringBoot

Time:01-01

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

CodePudding user response:

This is circular dependency issue you are facing.

Use the @JsonManagedReference and @JsonBackReference annotations to fix this issue.

Refer https://stackoverflow.com/a/47715121/6572971 for how to use this.

CodePudding user response:

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 mu problem, probably i used wrong.

  • Related