Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is join fetch in Spring Data JPA?
Join fetch is a way to tell JPA to load related entities together in a single query, avoiding multiple database calls and improving performance.
Click to reveal answer
beginner
Why use join fetch instead of lazy loading?
Join fetch loads related data eagerly in one query, preventing the 'N+1 select problem' where many extra queries are made for each related entity.
Click to reveal answer
intermediate
How do you write a JPQL query with join fetch?
Example: SELECT o FROM Order o JOIN FETCH o.items loads orders and their items in one query.
Click to reveal answer
intermediate
What problem does join fetch solve in ORM frameworks?
It solves the problem of multiple queries for related data, reducing database load and speeding up data retrieval.
Click to reveal answer
advanced
Can join fetch cause any issues? If yes, what kind?
Yes, join fetch can cause duplicate results or large result sets if used carelessly, especially with multiple collections. Use DISTINCT or limit joins.
Click to reveal answer
What does join fetch do in a Spring Data JPA query?
ACreates a new database table
BDelays loading related entities until accessed
CDeletes related entities automatically
DLoads related entities in the same query
✗ Incorrect
Join fetch loads related entities eagerly in one query to improve performance.
Which problem is commonly solved by using join fetch?
AData duplication in database
BN+1 select problem
CSyntax errors
DMemory leaks
✗ Incorrect
Join fetch prevents the N+1 select problem by loading related data in one query.
How do you write a join fetch in JPQL to load orders with their items?
ASELECT o FROM Order o JOIN FETCH o.items
BSELECT o FROM Order o LEFT JOIN o.items
CSELECT o FROM Order o WHERE o.items IS NOT NULL
DSELECT o FROM Order o JOIN o.items
✗ Incorrect
The JOIN FETCH keyword tells JPA to load the related items eagerly.
What is a potential downside of using join fetch with multiple collections?
ADuplicate results and large data sets
BSlower query compilation
CAutomatic data deletion
DNo effect on performance
✗ Incorrect
Join fetch with multiple collections can cause duplicates and large result sets.
Which keyword can help avoid duplicate results when using join fetch?
AORDER BY
BGROUP BY
CDISTINCT
DHAVING
✗ Incorrect
DISTINCT removes duplicate rows caused by join fetch.
Explain what join fetch is and why it improves performance in Spring Data JPA.
Think about how loading related data in one query helps.
You got /4 concepts.
Describe a scenario where using join fetch might cause problems and how to handle it.
Consider what happens when joining many related lists.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using JOIN FETCH in Spring Boot JPA queries?
easy
A. To create a new table for the joined entities
B. To delete related entities automatically when the parent is deleted
C. To load related entities eagerly in a single query and avoid multiple database hits
D. To update related entities in batch
Solution
Step 1: Understand what JOIN FETCH does
JOIN FETCH tells JPA to load related entities eagerly in the same query instead of lazy loading them later.
Step 2: Recognize the performance benefit
This reduces the number of database queries, improving performance by avoiding the N+1 select problem.
Final Answer:
To load related entities eagerly in a single query and avoid multiple database hits -> Option C
Quick Check:
Join fetch = eager load related data [OK]
Hint: Join fetch loads related data in one query to boost speed [OK]
Common Mistakes:
Thinking join fetch deletes or updates data
Confusing join fetch with creating new tables
Assuming join fetch delays loading entities
2. Which of the following is the correct JPQL syntax to fetch a parent entity and its child entities using join fetch?
easy
A. SELECT p FROM Parent p JOIN FETCH p.children
B. SELECT p FROM Parent p JOIN p.children FETCH
C. SELECT p FROM Parent p FETCH JOIN p.children
D. SELECT p FROM Parent p LEFT JOIN p.children FETCH
Solution
Step 1: Recall correct JPQL join fetch syntax
The correct syntax places JOIN FETCH before the association path: JOIN FETCH p.children.
Step 2: Check each option
Only SELECT p FROM Parent p JOIN FETCH p.children matches the correct syntax. The others misuse the order of keywords or use incorrect join types.
Final Answer:
SELECT p FROM Parent p JOIN FETCH p.children -> Option A
Quick Check:
Join fetch syntax = JOIN FETCH association [OK]
Hint: Remember: 'JOIN FETCH' comes together before the association [OK]
Common Mistakes:
Swapping FETCH and JOIN keywords
Placing FETCH after the association path
Using FETCH without JOIN keyword
3. Given the following JPQL query:
SELECT o FROM Order o JOIN FETCH o.items WHERE o.id = :id
What will happen when this query runs?
medium
A. It loads only the items without the Order
B. It loads the Order and all its items in one query, avoiding lazy loading
C. It throws a syntax error because JOIN FETCH cannot be used with WHERE
D. It loads only the Order, items are loaded lazily later
Solution
Step 1: Analyze the query structure
The query uses JOIN FETCH to eagerly load the items collection along with the Order entity filtered by id.
Step 2: Understand the effect of join fetch with WHERE
The WHERE clause filters the order, but the join fetch still loads the items eagerly in the same query.
Final Answer:
It loads the Order and all its items in one query, avoiding lazy loading -> Option B
Quick Check:
Join fetch + WHERE = eager load filtered data [OK]
Hint: Join fetch loads related data even with WHERE filters [OK]
Common Mistakes:
Thinking join fetch causes syntax errors with WHERE
Assuming items load lazily despite join fetch
Confusing join fetch with separate queries
4. Consider this JPQL query:
SELECT c FROM Customer c JOIN FETCH c.orders o WHERE o.status = 'PENDING'
What is the likely problem with this query?
medium
A. It may return duplicate Customer entities due to multiple matching orders
B. It will fail because JOIN FETCH cannot have an alias
C. It will not fetch orders eagerly because of the WHERE clause
D. It will only fetch orders with status other than 'PENDING'
Solution
Step 1: Understand join fetch with filtering on collection
Filtering on orders with WHERE o.status = 'PENDING' can cause multiple rows per customer if they have multiple pending orders.
Step 2: Recognize duplicate root entities issue
This leads to duplicate Customer entities in the result list unless distinct is used.
Final Answer:
It may return duplicate Customer entities due to multiple matching orders -> Option A
Quick Check:
Join fetch + filtered collection = possible duplicates [OK]
Hint: Filtering join fetch collections can cause duplicates [OK]
Common Mistakes:
Believing join fetch cannot have aliases
Thinking WHERE disables eager loading
Assuming only non-matching orders are fetched
5. You want to optimize loading a list of Author entities with their books and each book's publisher in one query. Which JPQL query correctly uses join fetch for this?
hard
A. SELECT a FROM Author a JOIN FETCH a.books, b.publisher
B. SELECT a FROM Author a JOIN a.books b JOIN FETCH b.publisher
C. SELECT a FROM Author a JOIN FETCH a.books JOIN b.publisher
D. SELECT a FROM Author a JOIN FETCH a.books b JOIN FETCH b.publisher
Solution
Step 1: Identify the need for nested join fetch
To load authors with books and each book's publisher eagerly, use join fetch on both associations.
Step 2: Check the syntax for multiple join fetches
SELECT a FROM Author a JOIN FETCH a.books b JOIN FETCH b.publisher correctly uses JOIN FETCH a.books b and then JOIN FETCH b.publisher to fetch nested associations.
Final Answer:
SELECT a FROM Author a JOIN FETCH a.books b JOIN FETCH b.publisher -> Option D