title: Spring Boot JPA StackOverflowError
body: <p>I am new at spring boot JPA. I am trying the examples described here:</p>
  
  <p><a href="https://en.wikibooks.org/wiki/Java_Persistence/OneToMany#Example_of_a_OneToMany_relationship_database" rel="nofollow noreferrer">https://en.wikibooks.org/wiki/Java_Persistence/OneToMany#Example_of_a_OneToMany_relationship_database</a></p>
  
  <p>so I have defined the two entities:</p>
  
  <pre><code>@Entity
  public class Employee {
    @Id
    @Column(name="EMP_ID")
    private long id;
    ...
    @OneToMany(mappedBy="owner")
    private List&lt;Phone&gt; phones;
    ...
  }
  </code></pre>
  
  <p>and:</p>
  
  <pre><code>@Entity
  public class Phone {
    @Id
    private long id;
    ...
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="OWNER_ID")
    private Employee owner;
    ...
  }
  </code></pre>
  
  <p>(an employee can have more than one phone number, work, home, etc.). The problem is that, when I try to list the Employee instances with my entityManager:</p>
  
  <pre><code>public List&lt;Employee&gt; getAll() 
  {
      return entityManager.createQuery("from Employee").getResultList();
  }
  </code></pre>
  
  <p>I got a spring StackOverflowError. Actually I am trying to refer Employee, which includes a list of Phone(s), but a Phone refers an Employee back so is it not a recursion? I feel as I am missing something about bi-directional associations (uni-directional associations worked fine) .. how can I have an entity A which refer B, being B referring A, and got a JSON response correctly from spring? Can someone point me in the right direction?  </p>
  
  <p>I found this workaround:</p>
  
  <p><a href="https://stackoverflow.com/questions/3325387/infinite-recursion-with-jackson-json-and-hibernate-jpa-issue">Infinite Recursion with Jackson JSON and Hibernate JPA issue</a></p>
  
  <p>using @JsonManagedReference and @JsonBackReference I can tell Jackson not to serialize one side on my association and it works! But the final result is a unidirectional association (I can see the list of phones on the employee entity, but not the employee on every phone instance). So what is the purpose of bi-directional association if I have to cut one side of them in order to have my code working? Very confused :)</p>