title: Spring MVC + Hibernate & Jackson = Could not write JSON: Infinite recursion
body: <p>I know this has been asked a lot of times, and the solution is pretty obvious, but in my case it doesn't really work, and I can't figure out how to solve it.</p>
  
  <p><strong>problem</strong>: <code>Could not write JSON: Infinite recursion (StackOverflowError)</code></p>
  
  <p>The setup is like this:</p>
  
  <p>Employee belongs to a Department. (ManyToOne)
  Department has a Employee as Manager. (OneToOne)</p>
  
  <p>I didn't want to have a @OneToMany List in Department so the owning side is missing.
  Employee has a Department object, but this is the Department it belongs to, not the Department he manages.</p>
  
  <p><strong>Employee.java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "ts_employee")
  //@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class,property="@emp_id")
  public class Employee extends AbstractEntity {
  
      @ManyToOne
      @JoinColumn(name = "dept_id")
      @JsonManagedReference
      private Department department;
  
      ... getters and setters
  }
  </code></pre>
  
  <p><strong>Department.java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "ts_department")
  //@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@dept_id")
  public class Department extends AbstractEntity {
  
      @OneToOne
      @JoinColumn (name = "manager_id")
      @JsonBackReference
      private Employee manager;
  
      .. other fields.. getters and setters
  }
  </code></pre>
  
  <p><strong>AbstractEntity.java</strong></p>
  
  <pre><code>@MappedSuperclass
  public class AbstractEntity implements Serializable {
  
      @Id
      @GeneratedValue()
      private Long id;
      ... getters and setters
  
  }
  </code></pre>
  
  <p>I've tried both solutions:</p>
  
  <ol>
  <li><p>@JsonBackReference + @JsonManagedReference
  I get rid of the StackOverflow, but the Department.manager is not serialized (@JsonBackReference), and not sent in the response, which in bad.</p></li>
  <li><p>@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@emp_id"), which doesn't seem to do anything, and StackOverflow is thrown in my face :(</p></li>
  </ol>
  
  <p>How can I solve this, hopefully without modifying the model classes?</p>
  
  <p>Thanks a lot :)</p>