N-plus-one appears when fetching a collection leads to additional queries for each item’s relationship. Diagnose query patterns, not just repository method names.
Before you start
You should know Java classes, dependency injection and basic HTTP requests. Identify where a call crosses a framework-managed boundary. The snippets illustrate a focused mechanism; database configuration, application wiring and authentication must be supplied by the surrounding application when applicable.
The practical goal is to reason through this situation: Listing orders and reading each customer’s name can trigger one extra query per order. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Record generated queries
Load a list and count subsequent relationship queries.
Step 2: Choose a focused fetch
Use a projection, entity graph or fetch join matching the response.
Step 3: Verify pagination semantics
Collection joins and result multiplication can complicate page size and query behavior.
Worked scenario
Listing orders and reading each customer’s name can trigger one extra query per order.
Twenty orders are loaded, then customer name access issues twenty additional reads. Eagerly loading every relationship may eliminate one symptom while transferring far more data. Optimize the specific listing contract and compare the resulting query count, rows and memory, including the paginated version.
Common mistake
Eager loading everything can exchange the problem for excessive joins and memory.
Verify the behavior
Increase list size and verify query growth is controlled without duplicate or missing page results.
Interview exercise
Fix a specific listing query.
Answer and reasoning
Use a suitable fetch join, entity graph or projection and verify pagination semantics and query count.
Continue learning
Compare the scenario with the Spring Boot interview questions and test your understanding with the Spring Boot MCQs. For terminology and implementation details, consult the reference material.