Demo: catching N+1 with a query-count test
On This Lesson 3 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
write a test that fails when queries multiply; prepares JNR-032-LAB1.
Draft outline details
Step by step
- Seed 100 orders with lines in a test.
- Add a query counter (Hibernate statistics or a datasource-proxy library; verify your choice).
- Call the order-history endpoint and print the count: 101.
- Write the assertion
queryCount <= 2; the test fails. - Planned mistake: add
fetch = EAGERand show the count is still high; revert it. - Add the
join fetchor entity graph; the test passes.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.