Pro Persistence — Chapter 8: Query Language (JPQL)¶
Book: Pro Jakarta Persistence in Jakarta EE 10 (Apress, 2023)
Chapter: 8 — Query Language (JPQL)
Overview¶
JPQL (Jakarta Persistence Query Language) is a SQL-like language that operates on the object model, not the relational model. You write queries using entity class names and field names — not table/column names. The JPA provider translates JPQL to dialect-specific SQL at runtime.
JPQL: SELECT n FROM ClusterNode n WHERE n.hostname = 'node-1'
SQL: SELECT * FROM cluster_node WHERE node_hostname = 'node-1'
SELECT and FROM Clauses¶
The FROM clause defines the domain using identification variables:
-- Full entity return (most common):
SELECT n FROM ClusterNode n
-- Single attribute:
SELECT n.hostname FROM ClusterNode n
-- Navigation through relationships (dot notation):
SELECT n.rack.datacenter.location FROM ClusterNode n WHERE n.id = :id
-- Multiple entity selection:
SELECT n, r FROM ClusterNode n LEFT JOIN n.rack r
Constructor Expressions — SELECT NEW¶
Project directly into a Java record/class without entity hydration or L1 cache overhead:
// Define the DTO:
public record NodeMetricSummaryDto(String hostname, Long recordCount, Double avgCpu) {
// Must have a matching public constructor
}
// JPQL constructor expression:
TypedQuery<NodeMetricSummaryDto> q = em.createQuery(
"SELECT NEW com.pulse.dto.NodeMetricSummaryDto(" +
" n.hostname, COUNT(t), AVG(t.cpuUsagePercent)" +
") " +
"FROM ClusterNode n LEFT JOIN n.telemetryRecords t " +
"GROUP BY n.hostname",
NodeMetricSummaryDto.class
);
List<NodeMetricSummaryDto> summaries = q.getResultList();
When to use SELECT NEW
- Read-only reporting endpoints — no need to track entities in L1 cache
- Dashboard aggregations — load only computed values
- Large result sets — minimize heap usage by projecting only needed columns
JOINs in JPQL¶
INNER JOIN¶
Returns only matching pairs — nodes that have a rack:
SELECT n FROM ClusterNode n JOIN n.rack r WHERE r.location = 'DC-01'
SELECT n.hostname, r.location FROM ClusterNode n INNER JOIN n.rack r
LEFT OUTER JOIN¶
Returns all entities on the left side even without a matching join:
-- Returns nodes even if they have no rack:
SELECT n, r FROM ClusterNode n LEFT JOIN n.rack r
SELECT n FROM ClusterNode n LEFT JOIN n.securityGroups sg WHERE sg IS NULL
JOIN FETCH — The N+1 Eliminator¶
Initializes lazy associations in a single SQL query, allowing safe detached traversal:
SELECT DISTINCT n FROM ClusterNode n
JOIN FETCH n.telemetryRecords t
JOIN FETCH n.rack r
WHERE n.status = :status
List<ClusterNode> nodes = em.createQuery(
"SELECT DISTINCT n FROM ClusterNode n " +
"JOIN FETCH n.telemetryRecords " +
"JOIN FETCH n.rack " +
"WHERE n.status = :status",
ClusterNode.class
)
.setParameter("status", NodeStatus.ACTIVE)
.getResultList();
em.close();
// Safe! Both telemetryRecords and rack are already initialized:
nodes.get(0).getTelemetryRecords().size(); // ✅ No LazyInitializationException
nodes.get(0).getRack().getLocation(); // ✅ No LazyInitializationException
flowchart LR
WITHOUT["Without JOIN FETCH<br/>N+1 Problem"]
W1["SELECT * FROM cluster_node"]
W2["SELECT * FROM telemetry WHERE node_id=1"]
W3["SELECT * FROM telemetry WHERE node_id=2"]
WN["... N more SELECT statements"]
WITH["With JOIN FETCH<br/>1 Query Total"]
JOIN["SELECT n.*, t.* FROM cluster_node n<br/>JOIN telemetry_record t ON t.node_id=n.id<br/>WHERE n.status='ACTIVE'"]
WITHOUT --> W1 --> W2 --> W3 --> WN
WITH --> JOIN
DISTINCT is required with JOIN FETCH on collections
Without DISTINCT, JPA returns duplicate parent entities (one per child row). For 3 nodes each with 5 telemetry records, you get 15 rows from the DB, producing 15 ClusterNode objects in the list (with duplicates). DISTINCT collapses them back to 3.
WHERE Clause — Conditional Expressions¶
Comparison & Range Operators¶
LIKE Pattern Matching¶
WHERE n.hostname LIKE 'node-%.cluster.local'
WHERE n.hostname LIKE '%primary%'
WHERE n.hostname NOT LIKE 'test-%'
-- Escape special characters:
WHERE n.hostname LIKE 'node\_01%' ESCAPE '\'
| Wildcard | Matches |
|---|---|
% |
Any sequence of characters (including empty) |
_ |
Exactly one character |
IS NULL / IS EMPTY¶
WHERE n.rack IS NULL -- no parent rack assigned
WHERE n.rack IS NOT NULL
WHERE n.telemetryRecords IS EMPTY -- collection has zero elements
WHERE n.telemetryRecords IS NOT EMPTY
IN Clause¶
WHERE n.status IN ('ACTIVE', 'MAINTENANCE')
WHERE n.status IN (:statusList) -- named parameter = Collection
WHERE n.rack.id NOT IN (1, 2, 3)
EXISTS — Subquery Existence Check¶
-- Nodes that have at least one critical telemetry record:
WHERE EXISTS (
SELECT t FROM TelemetryRecord t
WHERE t.node = n AND t.cpuUsagePercent > 95.0
)
ANY, ALL, SOME — Subquery Comparison¶
-- Nodes whose CPU is higher than ALL critical thresholds:
WHERE n.avgCpuPercent > ALL (
SELECT th.cpuThreshold FROM AlertThreshold th WHERE th.severity = 'CRITICAL'
)
-- Nodes matching ANY maintenance window:
WHERE n.id = ANY (
SELECT m.nodeId FROM MaintenanceSchedule m WHERE m.scheduledDate = :today
)
ORDER BY¶
-- Ascending (default):
SELECT n FROM ClusterNode n ORDER BY n.hostname
SELECT n FROM ClusterNode n ORDER BY n.hostname ASC
-- Descending:
SELECT n FROM ClusterNode n ORDER BY n.hardware.ramGb DESC
-- Multi-column sorting:
SELECT n FROM ClusterNode n
ORDER BY n.status ASC, n.hardware.ramGb DESC, n.hostname ASC
Aggregate Functions with GROUP BY + HAVING¶
| Function | JPQL | SQL Equivalent |
|---|---|---|
| Count | COUNT(n) or COUNT(n.id) |
COUNT(*) |
| Average | AVG(t.cpuUsagePercent) |
AVG(...) |
| Maximum | MAX(t.cpuUsagePercent) |
MAX(...) |
| Minimum | MIN(t.cpuUsagePercent) |
MIN(...) |
| Sum | SUM(t.bytesTransferred) |
SUM(...) |
-- Nodes grouped by status with average CPU per group:
SELECT n.status, COUNT(n), AVG(t.cpuUsagePercent), MAX(t.cpuUsagePercent)
FROM ClusterNode n
LEFT JOIN n.telemetryRecords t
GROUP BY n.status
ORDER BY COUNT(n) DESC
-- Only groups with more than 5 nodes and avg CPU above 70%:
SELECT n.rack.location, COUNT(n), AVG(t.cpuUsagePercent)
FROM ClusterNode n
JOIN n.telemetryRecords t
GROUP BY n.rack.location
HAVING COUNT(n) > 5 AND AVG(t.cpuUsagePercent) > 70.0
ORDER BY AVG(t.cpuUsagePercent) DESC
:material-hierarchy: Subqueries¶
Non-Correlated Subquery (independent)¶
-- Nodes in racks at a specific data center:
SELECT n FROM ClusterNode n
WHERE n.rack.id IN (
SELECT r.id FROM ServerRack r WHERE r.datacenter.location = 'DC-EAST'
)
Correlated Subquery (references outer query)¶
-- Nodes whose CPU is above the average for their rack:
SELECT n FROM ClusterNode n
WHERE n.avgCpuPercent > (
SELECT AVG(n2.avgCpuPercent) FROM ClusterNode n2
WHERE n2.rack = n.rack -- ← references outer n.rack
)
Bulk UPDATE and DELETE in JPQL¶
-- Bulk UPDATE:
UPDATE ClusterNode n
SET n.status = :newStatus, n.updatedAt = :now
WHERE n.rack.id = :rackId AND n.status = :oldStatus
-- Bulk DELETE:
DELETE FROM TelemetryRecord t
WHERE t.recordedAt < :cutoff AND t.node.status = 'DECOMMISSIONED'
Polymorphism and TREAT¶
JPQL queries are polymorphic by default — querying a superclass returns all subclass instances:
-- Returns ALL Project subtypes (DesignProject, QualityProject, etc.):
SELECT p FROM Project p WHERE p.budget > 50000
TREAT downcasting — access subclass-specific fields in a polymorphic query:
-- Filter by a field that only exists on QualityProject:
SELECT p FROM Project p
WHERE TREAT(p AS QualityProject).qaRating > 4
Key Takeaways — Chapter 8¶
- JPQL uses entity names and field names — never table/column names
JOIN FETCHcollapses N+1 selects into 1 query — always use withDISTINCTfor collection joinsSELECT NEWbypasses entity tracking — ideal for read-only projections and dashboardsIS EMPTYchecks collection relationships (no elements) — not the same asIS NULL- Subqueries can be correlated (reference outer query) or non-correlated (independent)
- JPQL is polymorphic by default — use
TREATto access subclass-specific fields - Bulk UPDATE/DELETE bypass L1 cache — always
em.clear()afterward
Ch7 — Using Queries | Ch9 — Criteria API | Back to Day 10 Index