0
0
Spring Bootframework~10 mins

Join fetch for optimization in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Join fetch for optimization
Start Query
Identify Entities to Fetch
Add JOIN FETCH Clause
Execute Single Query
Load Parent and Child Entities Together
Return Optimized Result
The flow shows how adding a JOIN FETCH clause in a query loads related entities in one go, avoiding multiple database hits.
Execution Sample
Spring Boot
String jpql = "SELECT p FROM Parent p JOIN FETCH p.children WHERE p.id = :id";
Parent parent = entityManager.createQuery(jpql, Parent.class)
  .setParameter("id", 1L)
  .getSingleResult();
This code fetches a Parent entity and its children in one query to optimize performance.
Execution Table
StepActionJPQL QueryDatabase Query ExecutedEntities LoadedResult
1Prepare JPQL with JOIN FETCHSELECT p FROM Parent p JOIN FETCH p.children WHERE p.id = :idNo query yetNoneJPQL ready
2Set parameter id=1Same as aboveNo query yetNoneParameter set
3Execute querySame as aboveSELECT p.*, c.* FROM Parent p JOIN Child c ON p.id = c.parent_id WHERE p.id = 1Parent with childrenSingle query returns parent and children
4Return resultSame as aboveQuery doneParent entity with children loadedParent object with children accessible
💡 Query executed once with JOIN FETCH, loading parent and children together, avoiding multiple queries.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
jpqlSELECT p FROM Parent p JOIN FETCH p.children WHERE p.id = :idSameSameSame
parameter idunsetunset111
parentnullnullnullParent entity with children loadedParent entity with children loaded
Key Moments - 2 Insights
Why do we add JOIN FETCH instead of just JOIN?
JOIN FETCH tells JPA to load the related entities eagerly in the same query, avoiding lazy loading multiple queries. See execution_table step 3 where one query loads both parent and children.
What happens if we omit JOIN FETCH and just use JOIN?
Without JOIN FETCH, JPA loads the parent first, then loads children lazily in separate queries, causing N+1 query problem. The execution_table shows only one query with JOIN FETCH.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the database query actually executed?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Database Query Executed' column in execution_table rows.
According to variable_tracker, what is the value of 'parent' after Step 3?
AParent entity with children loaded
Bnull
CJPQL query string
DParameter id value
💡 Hint
Look at the 'parent' row and 'After Step 3' column in variable_tracker.
If we remove JOIN FETCH from the JPQL, what changes in the execution flow?
AOne query loads parent and children together
BMultiple queries load children lazily after parent
CNo query is executed
DParameter setting fails
💡 Hint
Refer to key_moments explanation about JOIN vs JOIN FETCH.
Concept Snapshot
Join fetch in JPQL loads related entities eagerly in one query.
Syntax: SELECT p FROM Parent p JOIN FETCH p.children WHERE ...
Avoids multiple queries and N+1 problem.
Improves performance by fetching parent and children together.
Use when you know you need related data immediately.
Full Transcript
Join fetch is a way to optimize database queries in Spring Boot JPA. Instead of loading a parent entity and then lazily loading its children in separate queries, join fetch loads both parent and children in one query. The flow starts by preparing a JPQL query with JOIN FETCH, setting parameters, executing the query which runs a single SQL join query, and returning the parent entity with children loaded. Variables like the JPQL string, parameters, and the parent entity change state step by step. Key points include that JOIN FETCH triggers eager loading, avoiding multiple queries. The visual quiz tests understanding of when the query runs, variable states, and the effect of removing JOIN FETCH. This technique improves performance by reducing database hits.