Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4H5ph3J7k2MJnRuVH
one-to-many
Companion module for the ankurm.com article @OneToMany in Hibernate 7: Unidirectional vs Bidirectional, Set vs List and the SQL.
Every SQL statement and count in the article comes from a test here; each test writes what it observed to output/ and asserts the same facts.
Plain Hibernate on an in-memory H2: no Spring, no Docker.
Versions
| Component | Version | Notes |
|---|---|---|
| Hibernate ORM | 7.4.16.Final |
newest 7.x GA in maven-metadata.xml on 2026-11-16 (8.0.0.CR1 is listed after it but is not GA) |
| Jakarta Persistence | 3.2.0 |
|
| H2 | 2.4.240 |
in memory |
| JUnit / AssertJ | 6.0.3 / 3.27.7 |
|
| Java | 25 (Temurin 25.0.5+8) |
Run
./scripts/run-all.sh # or: mvn clean test
Tests and what they write
| Test | Output | Question it answers |
|---|---|---|
T01_ShapesTest |
output/01-shapes-h7.txt |
Which tables and which insert SQL do the three mapping shapes produce? |
T02_BagVsSetTest |
output/02-bag-vs-set-h7.txt |
What does removing ONE child cost, per mapping, with 5 and with 50 children? |
T03_SyncHelperTest |
output/03-sync-helper-h7.txt |
What does the database hold if only the parent side is updated? |
T04_FetchTest |
output/04-fetch-defaults-h7.txt, output/05-n-plus-one-h7.txt |
What do the fetch defaults load, and how many statements do N+1 and its fixes send? |
T05_MultipleBagTest |
output/06-multiple-bag-h7.txt |
When does MultipleBagFetchException fire, and what do the three fixes cost? |
T06_IdHashCodeTest |
output/07-id-hashcode-h7.txt |
Why does an id-based hashCode break Set.remove() after persist()? |
Entities: Department / Employee are the recommended shape; Variants holds every other mapping as small nested entities (public fields, so the mapping is all there is to read); EagerDepartment / EagerEmployee leave the fetch defaults untouched.