title: JPA many-to-many relationship causing infinite recursion and stack overflow error
body: <p>I'm working on an EclipseLink project in which one user can "follow" another as can be done on social media sites. I have this set up with a <code>User</code> entity (referencing a table called <code>users</code>) which has a list of "followers" (users who follow that user) and another list of "following" (users that user is following). The relationship is defined in a separate table called <code>followers</code> which contains columns for the followed user's ID (<code>user_id</code>) and the following user's ID (<code>follower_id</code>).</p>
  
  <p>My users model looks like this:</p>
  
  <pre><code>@Entity
  @Table(name = "users")
  @NamedQuery(name = "User.findAll", query = "SELECT u FROM USER u")
  public class User {
      // other attributes
      @ManyToMany(fetch = FetchType.LAZY)
      @JoinTable(name = "follower", joinColumns = @JoinColumn(
          name = "user_id", referencedColumnName = "id"),
      inverseJoinColumns = @JoinColumn(
          name = "follower_id", referencedColumnName = "id"))
      private List&lt;User&gt; followers;
  
      @ManyToMany(mappedBy = "followers")
      private List&lt;User&gt; following;
  
      // other getters and setters
      public List&lt;User&gt; getFollowers() {
          return this.followers;
      }
  
      public List&lt;User&gt; getFollowing() {
          return this.following;
      }
  }
  </code></pre>
  
  <p>The <code>getFollowers()</code> method seems to work fine, but when <code>getFollowing()</code> is called I get a bunch of console spam that culminates in a StackOverflowException:</p>
  
  <pre><code>com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion 
  (StackOverflowError) (through reference chain: 
  org.eclipse.persistence.indirection.IndirectList[0]-
  &gt;org.myproject.model.User["followers"]-
  &gt;org.eclipse.persistence.indirection.IndirectList[0]-
  &gt;org.myproject.model.User["following"]-
  ...
  at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase
  .serializeFields(BeanSerializerBase.java:518)
  at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize
  (BeanSerializer.java:117)
  at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer
  .serializeContents(IndexedListSerializer.java:94)
  at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer
  .serializeContents(IndexedListSerializer.java:21)
  ...
  </code></pre>
  
  <p>Please let me know if I should provide more of the stack trace. Any hints?</p>