title: JPA Hibernate, Lazy loading after a Join
body: <p>I need to get a entity and its collection from a Rest controler but I have a very strange behaviour with spring data JPA. </p>
  
  <p>I have a ManyToMany relation beetween to entities with a Lazy loading.</p>
  
  <pre><code>@Entity
  class Folder{
      ...
      @ManyToMany(fetch = FetchType.LAZY)
      protected Set&lt;Tag&gt; tags;
  
      ...getter setter
  }
  
  @Entity class Tag{
  
      @ManyToMany(fetch = FetchType.LAZY, mappedBy = "tags"
      protected List&lt;File&gt; files; 
  }
  </code></pre>
  
  <p>And a JPQL query in a spring jpa repository</p>
  
  <pre><code>@Query("SELECT f FROM Folder f INNER JOIN f.tags t WHERE
          f.id = :id")
  Folder findFolderById(@Param("id") int id);
  </code></pre>
  
  <p>So the Folder is supposed to be populated with the Tag collection and avoid the lazy loading part.</p>
  
  <p>But I can see in the logs that after the Join Query 
  there is a select on the Tag table, so a lazy loading.</p>
  
  <p>Hibernate: select folder0_.id as id2_2_...
  Hibernate: select tags0_.files_id ...</p>
  
  <p>I don't want to use fetch type EAGER because I don't always need the Tag collection. How can I avoid that Lazy loading ?</p>