title: JPA Criteria query eager fetch associated entities using a SINGLE query with joins instead of multiple queries
body: <p>We are moving from Hibernate native criteria to JPA criteria queries in scope of upgrading a hibernate from 4.3.11 to 5.2.12 and found out different behavior. Previously hibernate criteria use a single query with joins to eager fetch one-to-many associated entities, but JPA use separate queries to fetch the associated entities for each root entity.</p>
  
  <p>I know I can explicitly set fetch mode like <code>entityRoot.fetch("attributes", JoinType.INNER);</code> but we need to do it in some AbstractDao implementation that should work for any eager one-to-many association so can't explicitly set this.</p>
  
  <p>So can I somehow tell JPA criteria to eager fetch associated entities in a single query using joins by default instead of separate queries for each root entity?</p>
  
  <p>The code example:</p>
  
  <pre><code>    CriteriaBuilder builder = createCriteriaBuilder();
      CriteriaQuery&lt;T&gt; criteriaQuery = builder.createQuery(getEntityClass());
      Root&lt;T&gt; entityRoot = criteriaQuery.from(getEntityClass());
  
      criteriaQuery.select(entityRoot);
      criteriaQuery.where(builder.equal(entityRoot.get("param1"), "value"));
  
      return getEntityManager().createQuery(criteriaQuery).getResultList();
  </code></pre>