I have a Car entity and a Holder set in it.

 @Entity public class Car { // fields @ManyToMany @JoinTable(name = "holder_car", joinColumns = @JoinColumn(name = "car_id"), inverseJoinColumns = @JoinColumn(name = "holder_id")) @NotNull private Set<Holder> holder; 

Also here is an Entity Holder .

 @Entity public class Holder { // fields @ManyToMany(mappedBy = "holder") private Set<Car> cars; 

When I try to pull the Holder entity through the GET method, the answer goes into an "infinite loop". That is, the answer sends all the fields, and when it comes to the field, cars start issuing the fields of this entity, then reaching the field of holder and displaying its fields. And so on to infinity. It looks like this:

enter image description here

I read that it seems like Lazy-loading can help, but for some reason the answer comes the same. How can this be fixed?

  • Why do you use many to many connections? - GenCloud
  • @GenCloud Well, it would be more logical to make the connection one owner-several machines, but this does not change the situation, as I understand it - Pro100Denysko
  • Possible duplicate of the question: Infinite extraction from two sets - fori1ton
  • So take and do one-to-many, and to avoid recursion as a result of the query, remove the mapping from the Car and record the mapping in the Holder as @OneToMany (cascade = ALL) private Set <Car> cars; and do not use joins, the entity interpreter itself will understand where it needs to insert its keys (generates additional table in the database for communication) - GenCloud
  • one
    Return from the controller not Entity, but DTO with the required fields - Nick

0