title: Hibernate Many To One eager fetch N+1
body: <p>I have the following JPA Entity:</p>
  
  <pre><code>@Entity
  @Table(name = "app")
  public class App {
  
      @Id
      private String id;
  
      @OneToOne(mappedBy = "app")
      @PrimaryKeyJoinColumn
      private FileDetails fileDetails;
  
      @ManyToOne
      private Developer developer;
  
      @ManyToOne
      @JoinColumn(name = "category_id")
      private Category category;
  
      @OneToMany(mappedBy = "app", fetch = FetchType.EAGER)
      private List&lt;Image&gt; images;
  
      //getters, setters
  }
  </code></pre>
  
  <p>As you can see the Image entity is set to <code>EAGER</code> fetching.
  I want to select all entities using pagination and my Generic DAO uses this method:</p>
  
  <pre><code>public List&lt;T&gt; findByPage(int pageNum, int pageSize) {
          CriteriaQuery&lt;T&gt; cq = getEntityManager()
                  .getCriteriaBuilder().createQuery(entity);
          cq.select(cq.from(entity));
  
          int firstResult = pageNum * pageSize;
          return getEntityManager().createQuery(cq)
                  .setFirstResult(firstResult)
                  .setMaxResults(pageSize)
                  .getResultList();
  }
  </code></pre>
  
  <p>However when I run the query I have a N+1 in relation to the Image entity. So for each App selected I get N selects for the number of Images. Isnt EAGER fetching supposed to solve this problem? And what could be a solution?</p>