title: Spring Data JPA - bidirectional relation with infinite recursion
body: <p>First, here are my entities.</p>
  
  <p><strong>Player</strong> :</p>
  
  <pre><code>@Entity
  @JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, 
  property="id")
  public class Player {
  
      // other fields
  
      @ManyToOne
      @JoinColumn(name = "pla_fk_n_teamId")
      private Team team;
  
      // methods
  
  }
  </code></pre>
  
  <p><strong>Team</strong> :</p>
  
  <pre><code>@Entity
  @JsonIdentityInfo(generator=ObjectIdGenerators.UUIDGenerator.class, 
  property="id")
  public class Team {
  
      // other fields
  
      @OneToMany(mappedBy = "team")
      private List&lt;Player&gt; members;
  
      // methods
  
  }
  </code></pre>
  
  <p><strong>As many topics already stated, you can avoid the StackOverflowExeption in your WebService in many ways with Jackson.</strong></p>
  
  <p>That's cool and all but JPA still constructs an entity with infinite recursion to another entity before the serialization. This is just ugly ans the request takes much longer. Check this screenshot : <a href="https://i.stack.imgur.com/XOHdV.png" rel="noreferrer">IntelliJ debugger</a></p>
  
  <p>Is there a way to fix it ? Knowing that I want different results depending on the endpoint. Examples :</p>
  
  <ul>
  <li>endpoint <strong>/teams/{id}</strong> => Team={id..., members=[Player={id..., <strong>team=null</strong>}]}</li>
  <li>endpoint <strong>/members/{id}</strong> => Player={id..., team={id..., <strong>members=null</strong>}}</li>
  </ul>
  
  <p>Thank you!</p>
  
  <p><strong>EDIT : maybe the question isn't very clear giving the answers I get so I'll try to be more precise.</strong></p>
  
  <p>I know that it is possible to prevent the infinite recursion either with Jackson (@JSONIgnore, @JsonManagedReference/@JSONBackReference etc.) or by doing some mapping into DTO. The problem I still see is this : both of the above are post-query processing. The object that Spring JPA returns will still be (for example) a Team, containing a list of players, containing a team, containing a list of players, etc. etc.</p>
  
  <p><strong>I would like to know if there is a way to tell JPA or the repository (or anything) to not bind entities within entities over and over again?</strong></p>