I use JPA2 with it criteria API to select my objects from the database. OpenJPA implementation on WebSphere Application Server. All my objects are modeled using Fetchtype = Lazy.
I select an object with some criteria from the database and I want to immediately download all the nested data from the sub-tables. If I have a datamodel where table A is connected to oneToMany with table B, I can use the Fetch clause in my criteria query:
CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<A> cq = cb.createQuery(A.class); Root<A> root = cq.from(A.class); Fetch<A,B> fetch = root.fetch(A_.elementsOfB, JoinType.LEFT);
It works great. I get element A, and all of its elements B are filled correctly. Now table B has oneToMany related to table C, and I also want to load them. Therefore, I am adding the following query to my query:
Fetch<B,C> fetch2 = fetch.fetch(B_.elementsOfC, JoinType.LEFT);
But that will do nothing.
Does anyone know how to get layered objects in a single query?
Steven rudolf
source share