Infinite loop with onetomany / manytoone ? Spring / rest api

I chose to have a many-to-one relationship from the shelves to the warehouses! My problem is that when I add to the table with the shelves the foreign key of the table with the repositories then I get an ifnite loop with data, I tried to solve it with jsonignore / jsonmanagerefernce / jsonbackreference but then my server 415 has a problem. happen to any of you to help me? I think the connection of my tables is to blame but I do not know what to do ..

Entity warehouse:

@Entity
@Table(name="warehouse")
//@Access(AccessType.FIELD)
public class Warehouse {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name="id_warehouse")
        private Long id_warehouse;
    
        @Column(name="description_warehouse")
        private String description_warehouse;
    
        @OneToMany(mappedBy = "war_id")
        private List<Shelve> shelves = new ArrayList<>();
    
        @OneToMany(mappedBy = "war_id")
        private List<Management> managements = new ArrayList<>();
    
        public Warehouse() {
        }
//getter setter

Entity shelve:

@Entity
@Table(name = "shelve")
public class Shelve {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_shelve")
    private Long id_shelve;

    @Column(name = "description_shelve")
    private String description_shelve;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "war_id", referencedColumnName = "id_warehouse")
    private Warehouse war_id;

    public Shelve() {
}
//getter setter

ERROR:

Hibernate: select warehouse0_.id_warehouse as id_wareh1_2_0_, warehouse0_.description_warehouse as descript2_2_0_ from warehouse warehouse0_ where warehouse0_.id_warehouse=?
Hibernate: select shelves0_.war_id as war_id3_1_0_, shelves0_.id_shelve as id_shelv1_1_0_, shelves0_.id_shelve as id_shelv1_1_1_, shelves0_.description_shelve as descript2_1_1_, shelves0_.war_id as war_id3_1_1_ from shelve shelves0_ where shelves0_.war_id=?
2021-07-13 16:50:11.986  WARN 3968 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Failure while trying to resolve exception [org.springframework.http.converter.HttpMessageNotWritableException]

java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
    at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:472) ~[tomcat-embed-core-9.0.48.jar:9.0.48]
    at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.sendServerError(DefaultHandlerExceptionResolver.java:553) ~[spring-webmvc-5.3.8.jar:5.3.8]
    at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotWritable(DefaultHandlerExceptionResolver.java:443) ~[spring-webmvc-5.3.8.jar:5.3.8]
    at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException(DefaultHandlerExceptionResolver.java:210) ~[spring-webmvc-5.3.8.jar:5.3.8]
    at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:141) ~[spring-webmvc-5.3.8.jar:5.3.8]
    at org.springframework.web.servlet.handler.HandlerExceptionResolverComposite.resolveException(HandlerExceptionResolverComposite.java:80) ~[spring-webmvc-5.3.8.jar:5.3.8]
    at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1323) ~[spring-webmvc-5.3.8.jar:5.3.8]
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1134) ~[spring-webmvc-5.3.8.jar:5.3.8]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1080) ~[spring-webmvc-5.3.8.jar:5.3.8]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963) ~[spring-webmvc-5.3.8.jar:5.3.8]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.8.jar:5.3.8]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.8.jar:5.3.8]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:655) ~[tomcat-embed-core-9.0.48.jar:4.0.FR]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.8.jar:5.3.8]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:764) ~[tomcat-embed-core-9.0.48.jar:4.0.FR]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:228) ~[tomcat-embed-core-9.0.48.jar:9.0.48]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:163) ~[tomcat-embed-core-9.0.48.jar:9.0.48]

Solution 1:

You have wrong connections, you need a total refactor and understand the main ideas of the relationship's please look the link bellow:

https://dev.to/jhonifaber/hibernate-onetoone-onetomany-manytoone-and-manytomany-8ba