This repository contains the demo code for the JUG Bonn talk "Stop guessing. Start testing.: Hibernate-Queries im Griff".
It shows how to make Hibernate and JPA behavior visible in integration tests by capturing and asserting the generated SQL, so that common persistence and performance problems can be detected early instead of being discovered through log inspection or in production.
Among others, the examples cover:
- query capturing in integration tests
LazyInitializationException- the pitfalls of
open-in-view - the N+1 query problem
- fetch strategies such as
JOIN FETCH,@EntityGraph, and@BatchSize - in-memory pagination caused by collection fetches
- redundant
save()calls and unnecessary merges
To run the demo locally, you need:
- JDK 25
- Docker running locally (required by Testcontainers)
Start the test suite:
./gradlew testThe integration tests start PostgreSQL via Testcontainers and compare the captured SQL with the validation files in data/test/validation.
src/main/java/de/cronn/hibernatetalk/Application.java: demo application and Hibernate/JPA examplessrc/test/java/de/cronn/hibernatetalk/IntegrationTest.java: end-to-end tests for the demo scenariossrc/test/java/de/cronn/hibernatetalk/test/QueryValidationTraits.java: query capture and assertion helpersdata/test/validation: expected SQL snapshots used by the tests
The progression of the talk can be followed through the commit history. Each commit corresponds to a step in the live demo:
Initial commitCapturingN+1@BatchSizeJoin fetchEntityGraphOpen in viewRedundant saveHibernate dirty checkingIn-Memory PaginationFail on in-memory pagination and @BatchSize
A convenient way to walk through the demo is:
git log --oneline --reverse