title: Springboot Hibernate One to Many infinty recursion
body: <p>I have two SQL tables as follow:</p>
  
  <p><strong>users</strong><br>
  id<br>
  username<br>
  password<br>
  ...and more irrelevant stuff</p>
  
  <p><strong>activities</strong><br>
  id<br>
  name<br>
  description<br>
  users</p>
  
  <p>I created two entities classes with their Repository (with the anotation @RepositoryRestResource)</p>
  
  <pre><code>@Entity
  public class Users {
      @Id
      private int id;
      private String username;
      private String password;
  
      @OneToMany(mappedBy = "users")
      private Set&lt;Activities&gt; activities;
  
      public Set&lt;Activities&gt; getActivities() {
          return activities;
      }
  
  //constructors &amp;&amp; getters setters
  </code></pre>
  
  <p>and</p>
  
  <pre><code>
  @Entity
  public class Activities {
  
      @Id
      private int id;
  
      @ManyToOne
      @JoinColumn(name = "users")
      private Users users;
  
      private String name;
  
      private String description;
  
  //constructors &amp;&amp; get
  </code></pre>
  
  <p>I created an endpoint to test the workflow, by creating an activity. It's all hardcoded as follow</p>
  
  <pre><code> @RequestMapping("/calendar")
      public Set&lt;Activities&gt; goToCalendar(Authentication authentication){
          Users user = unbreakableService.isAuth(authentication);
          Activities activities = new Activities(user,"karate", "entrenobueno");
          activitiesRespository.save(activities);
          return usersRepository.findByUsername(user.getUsername()).getActivities();
      }
  </code></pre>
  
  <p>The code is saving the activity correctly, but, when it's supposed to return the list of activities, it returns an infinity recursive information, like:</p>
  
  <pre><code>[{"id":161,"users":{"id":121,"username":"Ojka","password":"$2a$10$Z/bj.5JvQmPquJ67fqF.leUyBkkXJ/2x3z/.bUon8YsIAdV8HMnoG","email":"ojka","pushup":99999,"pullup":null,"handstandhold":9999,"handstandpushup":null,"frontlever":null,"backlever":null,"activities":[{"id":161,"users":{"id":121,"username":"Ojka","password":"$2a$10$Z/bj.5JvQmPquJ67fqF.leUyBkkXJ/2x3z/.bUon8YsIAdV8HMnoG","email":"ojka","pushup":99999,"pullup":null,"handstandhold":9999,"handstandpushup":null,"frontlever":null,"backlever":null,"activities":[{"id":161,"users":{"id":121,"username":"Ojka","password":"$2a$10$Z/bj.5JvQmPquJ67fqF.leUyBkkXJ/2x3z/.bUon8YsIAdV8HMnoG","email":"ojka","pushup":99999,"pullup":null,"handstandhold":9999,"handstandpushup":null,"frontlever":null,"backlever":null,"activities":[{"id":161,"users":{"id":121,"username":"Ojka","password":"$2a$10$Z/bj.5JvQmPquJ67fqF.leUyBkkXJ/2x3z/.bUon8YsIAdV8HMnoG","email":"ojka","pushup":99999,"pullup":null,"handstandhold":9999,"handstandpushup":null,"frontl
  </code></pre>
  
  <p>I can't read the whole log since the console doesn't show that much information.
  This is the first error I can see</p>
  
  <pre><code> Failure while trying to resolve exception [org.springframework.http.converter.HttpMessageNotWritableException]
  
  java.lang.IllegalStateException: Cannot call sendError() after the response has been committed.
  </code></pre>
  
  <p>And this is the last line of the log</p>
  
  <pre><code>2020-01-25 20:48:54.795 ERROR 19924 --- [nio-8080-exec-2] s.e.ErrorMvcAutoConfiguration$StaticView : Cannot render error page for request [/api/calendar] and exception [Could not write JSON: Infinite recursion (StackOverflowError); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.unbreakable.unbreakable.persistance.Users["activities"]-&gt;org.hibernate.collection.internal.PersistentSet[0]-&gt;com.unbreakable.unbreakable.persistance.Activities["users"]-&gt;com.unbreakable.unbreakable.persistance.Users["activities"]-&gt;org.hibernate.collection.internal.Persi....
  </code></pre>
  
  <p>If I check the table activities (SELECT * FROM ACTIVITIES) there's no infinity information. Just a few activities (the ones I created testing it).</p>
  
  <p>I believe, it should return a Set of activities that the user has. </p>