Day 10 — Dynamic JPQL Queries & Criteria API¶
Daily Time Investment: 2.5 hours | Week: 2 | Phase: 3
Daily Schedule¶
| Segment | Duration | Activity |
|---|---|---|
| Core Theory | 45 min | JPQL join fetches, SELECT NEW DTO projections, GROUP BY/HAVING, CriteriaBuilder type-safe construction |
| Book Reading | 30 min | Pro Persistence Ch7 (Using Queries) + Ch8 (JPQL Language) + Ch9 (Criteria API) |
| Hands-On Lab | 75 min | Dynamic multi-field search service with Criteria API; verify N+1 elimination with JOIN FETCH |
Files in This Day¶
-
Pro Persistence Ch7 — Using Queries
createQuery(),TypedQuery<T>, named/positional parameters,@NamedQuery(pre-compiled),getSingleResult()vsgetResultList()vsgetResultStream(),setFirstResult()/setMaxResults()pagination,executeUpdate()bulk ops, native SQL, query hints. -
Pro Persistence Ch8 — Query Language (JPQL)
JPQL vs SQL,
SELECT NEWconstructor expressions,INNER JOIN/LEFT OUTER JOIN/JOIN FETCH,BETWEEN/LIKE/IS EMPTY/EXISTS,GROUP BY+HAVING, correlated subqueries, polymorphism,TREATdowncasting, bulkUPDATE/DELETE. -
Pro Persistence Ch9 — Criteria API
CriteriaBuilder,CriteriaQuery<T>,Root<T>,Predicatecomposition, dynamic multi-field search pattern,Join/ fetch joins,Tupleprojections,cb.construct()DTO,ORDER BY/GROUP BY/HAVING, correlated subqueries (correlate()),CriteriaUpdate/CriteriaDelete, canonical metamodel. -
Lab Guide — JPQL & Criteria API
@NamedQuerystatic queries,NodeMetricSummaryDtoprojection viaSELECT NEW, N+1 elimination withJOIN FETCH,GROUP BY + HAVINGoverload detection,CriteriaBuildermulti-predicate paginated search.
Prerequisites to Continue¶
New concepts not seen in Phase 1 or Phase 2
- JPQL vs SQL — JPQL operates on the object model (entity class names and field names), not table/column names;
FROM ClusterNode cnotFROM cluster_node c - N+1 Select Problem — loading N parent entities each triggering a separate SELECT for their lazy children = N+1 total queries;
JOIN FETCHcollapses this into 1 query TypedQuery<T>— type-safe version ofQuery; avoids unchecked casts ongetResultList()SELECT NEW com.pulse.dto.NodeMetricSummaryDto(...)— constructor expression; bypasses entity hydration and First-Level Cache entirely; must match a public constructorCriteriaBuilder— the factory for type-safe programmatic queries; obtained fromem.getCriteriaBuilder()Root<T>— represents the entity being queried (FROMclause); similar toFROM Employee ein JPQLPredicate— the type-safe equivalent of a JPQLWHEREcondition; can be composed withcb.and()/cb.or()