Hibernate statistics
On This Lesson 4 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
measure statements and cache use in tests.
Planned topics
hibernate.generate_statistics=trueexposes counts of statements, entity loads, flushes, and cache hits.- Assert statement counts in tests to catch N+1 regressions.
- Log SQL with
org.hibernate.SQLat debug level rather thanshow-sqlin production. - Libraries such as datasource-proxy can also count statements (verify choice before recording).
Draft outline details
Demonstration
Statistics stats = sessionFactory.getStatistics();
stats.clear();
service.loadOrders();
assertThat(stats.getPrepareStatementCount()).isLessThanOrEqualTo(2);Check your understanding
Why assert statement counts in tests?
Show answer
To catch N+1 queries before production.
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.