Module 2B / Lesson 2B.1

Hibernate Mental Model Tests

Map entities after the schema exists and prove Hibernate behavior in labeled tests.

Concept

Hibernate is easiest to learn when each claim becomes a tiny proof. Phase 2B maps entities onto the already-owned SQL model, then uses tests to show transient, managed, detached, removed, dirty-checking, lazy-loading, transaction, and equality behavior.

Task

  1. Add JPA entities for cinemas, screens, movies, and showtimes.
  2. Keep the database schema under Flyway; do not use ddl-auto as a schema authoring tool.
  3. Write educational tests for entity states, persistence context identity, dirty checking, lazy boundaries, and equality consequences.
  4. Capture an annotated SQL-log transcript for the article.

Run

./gradlew dockerTest --tests "*HibernateMentalModelEducationalTest"

Expected Result

  • The educational test suite names each Hibernate behavior.
  • The transcript explains emitted SQL instead of dumping logs without interpretation.

Common Traps

  • Letting Hibernate generate the schema after Phase 2A proved the invariants in SQL.
  • Equating lazy loading with performance without naming the transaction boundary.
  • Writing exact query-count assertions in tests that are meant to teach behavior.

Hint Ladder

Hint 1

Start with the persistence context. Most Hibernate surprises flow from identity and unit-of-work rules.

Hint 2

A lazy exception is useful here because it pins the boundary learners need to recognize.

Hint 3

Entity equals/hashCode is a Java collection story as much as a database story.

Solution

See HibernateMentalModelEducationalTest.java and phase-2b-hibernate-sql-log.txt.

Boot 3 to 4 Delta

Boot 4 does not remove the core JPA mental model. The useful delta is dependency shape and Jakarta-era package names; the persistence-context rules remain the lesson.