We move from the native Hibernate criteria to the JPA query criteria in the area of updating sleep mode from 4.3.11 to 5.2.12 and we find different behavior. Previously, sleep criteria used a single query with joins to obtain one-to-many selections, but JPA uses separate queries to retrieve related objects for each root object.
I know that I can explicitly set the sampling mode, for example entityRoot.fetch("attributes", JoinType.INNER);, but we need to do this in some AbstractDao implementation that should work for any impatient one-to-many relationship, so I cannot explicitly set this.
So, is there any way to define JPA criteria for finding related objects in a single query using default joins instead of separate queries for each root object?
Code example:
CriteriaBuilder builder = createCriteriaBuilder();
CriteriaQuery<T> criteriaQuery = builder.createQuery(getEntityClass());
Root<T> entityRoot = criteriaQuery.from(getEntityClass());
criteriaQuery.select(entityRoot);
criteriaQuery.where(builder.equal(entityRoot.get("param1"), "value"));
return getEntityManager().createQuery(criteriaQuery).getResultList();
source
share