title: Hibernate infinite loop recursion in many to many relation
body: <p>I want to create bidirectional student-discipline relation. Anything works fine until I've enrolled user to discipline. Now I'm getting infinite recursion.</p>
  
  <p>Classes looks like this:</p>
  
  <pre><code>//Student.java
  @Entity
  @Table(name = "students")
  public class Student {
      @NotNull
      @Id
      @Column(name = "STUDENT_ID")
      private String id;
      private String name;
      @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
      @JoinTable(name = "students_disciplines", joinColumns = @JoinColumn(name = "STUDENT_ID"), inverseJoinColumns = @JoinColumn(name = "DISCIPLINE_ID"))
      @JsonSerialize(using = NestedDisciplineSetSerializer.class)
      private Set&lt;Discipline&gt; disciplines = new HashSet&lt;&gt;();
      //Getters, Setters
  }
  //NestedDisciplineSetSerializer.java
  public class NestedDisciplineSetSerializer extends JsonSerializer&lt;Set&lt;Discipline&gt;&gt; {
      @Override
      public void serialize(Set&lt;Discipline&gt; value, JsonGenerator jgen, SerializerProvider p) throws IOException, JsonProcessingException {
          jgen.writeStartArray();
          for (Discipline s : value) {
              jgen.writeStartObject();
              jgen.writeStringField("name", s.getName());
              jgen.writeNumberField("id", s.getId());
              jgen.writeBooleanField("recommended", s.isRecommended());
              jgen.writeEndObject();
          }
          jgen.writeEndArray();
      }
  }
  //Discipline.java
  @Entity`enter code here`
  @Table(name = "disciplines")
  public class Discipline {
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Id
      @Column(name = "DISCIPLINE_ID")
      private int id;
      @NotNull
      private String name;
      @NotNull
      private int credits;
      private String annotation;
      private boolean recommended;
      @ManyToMany(mappedBy = "disciplines", fetch = FetchType.EAGER)
      @JsonSerialize(using = NestedStudentSetSerializer.class)
      private Set&lt;Student&gt; students = new HashSet&lt;&gt;();
      //Getters, Setters
  }
  //NestedStudentSetSerializer.java
  public class NestedStudentSetSerializer extends JsonSerializer&lt;Set&lt;Student&gt;&gt; {
      @Override
      public void serialize(Set&lt;Student&gt; value, JsonGenerator jgen, SerializerProvider p) throws IOException, JsonProcessingException {
          jgen.writeStartArray();
          for (Student s : value) {
              jgen.writeStartObject();
              jgen.writeStringField("name", s.getName());
              jgen.writeStringField("id", s.getId());
              jgen.writeEndObject();
          }
          jgen.writeEndArray();
      }
  }
  </code></pre>
  
  <p>Error appears to be while loading Set after <code>Student s = sDao.findOne(id);</code></p>
  
  <p>Tried to search here, but was not able to find right solution. Anything made the same result.</p>
  
  <p><a href="http://pastebin.com/JZ6Xd68N" rel="nofollow noreferrer">Error log</a></p>