Reputation: 1750
Please, my JPA Query is created using CriteriaQuery and CriteriaBuilder like this:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<TaskInfo> cq = cb.createQuery(TaskInfo.class);
Root<TaskInfo> from = cq.from(TaskInfo.class);
cq.select(from);
...
from.get("task").get("id").in(taskList)
...
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.
A way to do it is this
for(TaskInfo ti : tiList){
ti.getTask().getTaskData().getSomethingElse().getX();
}
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 & cheers
Thanks for the help. But Task has a property
PeopleAssignments peopleAssignments
and PeopleAssignments has a property
List<OrganizationalEntity> businessAdministrators
so this works
from.fetch("task").fetch("peopleAssignments") // Returns a SingularAttributeJoin
but if I do this
from.fetch("task").fetch("peopleAssignments").fetch("businessAdministrators")
I get:
java.lang.ClassCastException: org.hibernate.ejb.metamodel.SingularAttributeImpl cannot be cast to javax.persistence.metamodel.ManagedType
I think it's because this time I'm trying to fetch List. Do you know how to fix this?
Upvotes: 1
Views: 1735
Reputation: 20142
you can add a fetch()
to your query to guarantee the load of other lazy linked objects as described here in chapter 5.6.2 or here in chapter 9.3.3.
Root<TaskInfo> from = cq.from(TaskInfo.class);
Join<TaskInfo, Task> task = from.fetch("task");
task.fetch("id");
...
I did not test it by myself, so I hope this works.
Upvotes: 1