title: JPA : Forcing eager load only for a query
body: <p>Please, my JPA Query is created using CriteriaQuery and CriteriaBuilder like this:</p>
  
  <pre><code>CriteriaBuilder cb = em.getCriteriaBuilder();
  CriteriaQuery&lt;TaskInfo&gt; cq = cb.createQuery(TaskInfo.class);
  Root&lt;TaskInfo&gt; from = cq.from(TaskInfo.class);
  cq.select(from);
  ...
  from.get("task").get("id").in(taskList)
  ...
  </code></pre>
  
  <p>I want to make sure that I eagerly load the whole graph of objects so that later on I don't have proxies instead of objects when I close the JPA Session.:
  TaskInfo->Task->TaskData->and so on until object X.</p>
  
  <p>A way to do it is this</p>
  
  <pre><code>for(TaskInfo ti : tiList){
      ti.getTask().getTaskData().getSomethingElse().getX();
  }
  </code></pre>
  
  <p>But this sucks, right? And I don't want to change fetching in general, but only for this specific query. How can I do it? Thanks &amp; cheers</p>
  
  <hr>
  
  <p>Thanks for the help. But
  Task has a property</p>
  
  <pre><code>PeopleAssignments peopleAssignments
  </code></pre>
  
  <p>and PeopleAssignments has a property</p>
  
  <pre><code>List&lt;OrganizationalEntity&gt; businessAdministrators
  </code></pre>
  
  <p>so this works</p>
  
  <pre><code>from.fetch("task").fetch("peopleAssignments") // Returns a SingularAttributeJoin
  </code></pre>
  
  <p>but if I do this</p>
  
  <pre><code>from.fetch("task").fetch("peopleAssignments").fetch("businessAdministrators")
  </code></pre>
  
  <p>I get:</p>
  
  <p>java.lang.ClassCastException: org.hibernate.ejb.metamodel.SingularAttributeImpl cannot be cast to javax.persistence.metamodel.ManagedType</p>
  
  <p>I think it's because this time I'm trying to fetch List. Do you know how to fix this?</p>