0
0
Spring Bootframework~10 mins

N+1 query problem in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the association annotation to fetch associated customers eagerly when fetching orders.

Spring Boot
@ManyToOne(fetch = [1])
private Customer customer;
Drag options to blanks, or click blank then click option'
AFetchType.EAGER
BFetchType.LAZY
CFetchType.DEFAULT
DFetchType.NONE
Attempts:
3 left
💡 Hint
Common Mistakes
Using LAZY fetch causes multiple queries for each related entity.
Using DEFAULT or NONE does not explicitly solve the N+1 problem.
2fill in blank
medium

Complete the JPQL query to fetch orders with customers using a join fetch to avoid N+1 queries.

Spring Boot
SELECT o FROM Order o [1] JOIN FETCH o.customer
Drag options to blanks, or click blank then click option'
ALEFT
BFULL
CINNER
DRIGHT
Attempts:
3 left
💡 Hint
Common Mistakes
Using LEFT JOIN FETCH might include orders without customers.
RIGHT and FULL joins are rarely used in JPQL and may cause errors.
3fill in blank
hard

Fix the error in the repository method to prevent N+1 queries by adding the correct annotation.

Spring Boot
@Query("SELECT o FROM Order o [1] JOIN FETCH o.customer")
List<Order> findAllOrdersWithCustomers();
Drag options to blanks, or click blank then click option'
AINNER
BLEFT
CRIGHT
DOUTER
Attempts:
3 left
💡 Hint
Common Mistakes
Using RIGHT or OUTER join fetch causes JPQL syntax errors.
Omitting JOIN FETCH causes lazy loading and N+1 queries.
4fill in blank
hard

Fill both blanks to define a Spring Data JPA method that fetches orders with customers eagerly.

Spring Boot
List<Order> findAllBy[1]Fetch[2]();
Drag options to blanks, or click blank then click option'
ACustomer
BOrder
CEagerly
DLazy
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Order' in the first blank is incorrect because the method targets customers.
Using 'Lazy' in the second blank does not solve N+1 queries.
5fill in blank
hard

Fill all three blanks to complete the code that uses EntityGraph to solve the N+1 query problem.

Spring Boot
@EntityGraph(attributePaths = {"[1]"})
List<Order> findAllWith[2]();

// Usage:
List<Order> orders = orderRepository.[3]();
Drag options to blanks, or click blank then click option'
Acustomer
BCustomers
CfindAllWithCustomers
DfindAll
Attempts:
3 left
💡 Hint
Common Mistakes
Using plural 'customer' in attributePaths causes errors.
Calling 'findAll()' does not use the EntityGraph and causes N+1 queries.