title: Java Hibernate json infinite recursion with self referencing class
body: <p>class employee:</p>
  
  <pre><code>@Entity
  @Table(name = "Employee")
  public class Employee {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "EmployeeID")
      private int EmployeeID;
  
      @Column(name = "ManagerID")
      private Integer ManagerID;
  
      @ManyToOne(cascade={CascadeType.ALL})
      @JoinColumn(name="ManagerID", insertable = false, updatable = false)
      @JsonBackReference
      private Employee manager;
  
      @OneToMany(mappedBy="manager")
      @JsonManagedReference
      private Set&lt;Employee&gt; employees;
  
      @ManyToOne(cascade={CascadeType.ALL})
      @JoinColumn(name = "DepartmentID")
      private Department department;
  
      @ManyToOne(cascade={CascadeType.ALL})
      @JoinColumn(name = "SalaryTypeID")
      private SalaryType salaryType;
  
      @Column(name = "Name")
      private String Name;
      //setters and getters here, wont be posting them
  }
  </code></pre>
  
  <p>Whenever I create an instance of employee I get this infinite json error: </p>
  
  <pre><code>SEVERE: Servlet.service() for servlet [SpringMVC] in context with path 
  [/SpringMVC] threw exception [Handler processing failed; nested exception is 
  java.lang.StackOverflowError] with root cause
  java.lang.StackOverflowError
  at java.nio.CharBuffer.&lt;init&gt;(Unknown Source)
  at java.nio.HeapCharBuffer.&lt;init&gt;(Unknown Source)
  at java.nio.CharBuffer.wrap(Unknown Source)
  at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
  at sun.nio.cs.StreamEncoder.write(Unknown Source)
  at sun.nio.cs.StreamEncoder.write(Unknown Source)
  at java.io.OutputStreamWriter.write(Unknown Source)
  at java.io.Writer.write(Unknown Source)
  at com.google.gson.stream.JsonWriter.string(JsonWriter.java:534)
  at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:402)
  at com.google.gson.stream.JsonWriter.value(JsonWriter.java:495)
  </code></pre>
  
  <p>(and then it keeps going)</p>
  
  <p>Since I am self referencing manager which is an employee in the employee class, how can I fix this?</p>