title: Why spring jpa repository always load eagerly in @GetMapping method?
body: <p>I want to create a simple many-to-many relation by using spring jpa and hibernate, here's the code:</p>
  
  <pre><code>@Entity
  public class Book {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private int id;
  
      @Column(name = "name")
      private String name;
  
      @ManyToMany(cascade = CascadeType.ALL,
      fetch = FetchType.LAZY)
      @JoinTable(name = "book_publisher",
          joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"),
          inverseJoinColumns = @JoinColumn(name = "publisher_id", referencedColumnName = "id"))
      private Set&lt;Publisher&gt; publishers;
  </code></pre>
  
  <p>and </p>
  
  <pre><code>@Entity
  public class Publisher {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private int id;
  
      private String name;
  
      @ManyToMany(mappedBy = "publishers"
      , fetch = FetchType.LAZY)
      @Nullable
      private Set&lt;Book&gt; books = new HashSet&lt;&gt;();
  
      public Publisher(String name) {
          this.name = name;
      }
  </code></pre>
  
  <p>The collections are all lazily fetched as the code shown above.</p>
  
  <p>And I create two repositories of there two entities:</p>
  
  <pre><code>public interface PublisherRepository extends JpaRepository&lt;Publisher, Integer&gt;{
  }
  </code></pre>
  
  <pre><code>public interface BookRepository extends JpaRepository&lt;Book, Integer&gt;{
  
  }
  </code></pre>
  
  <p>And I create a simple controller:</p>
  
  <pre><code>    @GetMapping("/getPublisher/{id}")
      public Publisher getPublisher(@PathVariable Integer id) {
          return publisherRepository.findById(id).get();
      }
  </code></pre>
  
  <p>something strange happens:</p>
  
  <p>when I make a http call via curl, I received such response:</p>
  
  <p>{"id":1,"name":"YanYan","books":[{"id":1,"name":"Avengers","publishers":[{"id":1,"name":"YanYan","books":[{"id":1,"name":"Avengers","publishers":[{"id":1,"name":"YanYan","books":[{"id":1,"name":"Avengers","publishers":[{"id":1,"name":"YanYan","books":[{"id":1,"name":"Avengers","publishers":[{"id":1,"name":"YanYan","books":[{"id":1,"name":"Avengers","publishers":[{"id" .....</p>
  
  <p>which indicates none of them are lazily fetched, this causes a infinite loop.</p>
  
  <p>Can anyone tell me why?</p>