3.9 KiB
hibernate-demo
Companion repository for three ankurm.com posts on Hibernate 7's persistence-context APIs:
get() vs load()/getReference(), merge() vs refresh(), and inserting objects
efficiently. Every claim in those posts that comes from this repo traces to a script here and a
captured transcript in docs/output/ — nothing is asserted that wasn't actually run.
Versions
| Component | Version |
|---|---|
| Hibernate ORM | 7.4.1.Final (GA 2026-06-09) |
| Spring Boot | 4.1.0 (GA 2026-06-10) |
| Java | 25 (LTS) |
| H2 | in-memory, managed by Spring Boot |
See docs/00-versions.md for how these were verified and a trap worth
knowing about if you bump the Spring Boot version.
Quickstart
git clone https://ankurm.com/git.app/asmhatre/hibernate-demo.git
cd hibernate-demo
./scripts/run.sh getvsload
Requires JDK 25 and a network connection the first time (Maven needs to fetch plugins online
before -o offline mode works for subsequent runs).
Profiles
Each profile is a CommandLineRunner against an in-memory H2 database. There is no web server —
every run starts, executes its scenario, prints the result, and exits.
| Profile | Runs | Chapter |
|---|---|---|
getvsload |
session.get() vs session.getReference(), proxies, LazyInitializationException |
docs/01-get-vs-load.md |
mergerefresh |
merge() vs refresh() against a @Version-ed entity |
docs/02-merge-vs-refresh.md |
insert-identity |
Batch insert attempt with GenerationType.IDENTITY |
docs/03-inserting-objects.md |
insert-sequence |
The same insert, with GenerationType.SEQUENCE |
docs/03-inserting-objects.md |
./scripts/run.sh getvsload
./scripts/run.sh mergerefresh
./scripts/run.sh insert-identity
./scripts/run.sh insert-sequence
Regenerating captured output
./scripts/run-all.sh
Regenerates every file in docs/output/ from a real run. scripts/clean_output.py strips JVM
noise and a harmless duplicate SQL echo line so the committed transcripts stay readable —
nothing else is edited by hand.
Documentation index
| Chapter | Covers |
|---|---|
| 00 — Versions | Verified version pins, and the Spring Boot patch that silently changes which Hibernate patch you get |
| 01 — get() vs load() | Proxy deferral, LazyInitializationException, proxy identity vs equals() |
| 02 — merge() vs refresh() | Optimistic locking, which method fails loudly vs silently |
| 03 — Inserting objects | IDENTITY vs SEQUENCE and JDBC batching, with hibernate.generate_statistics as evidence |
Captured output index
| File | Scenario |
|---|---|
| docs/output/get-vs-load.txt | getvsload |
| docs/output/merge-vs-refresh.txt | mergerefresh |
| docs/output/insert-identity.txt | insert-identity |
| docs/output/insert-sequence.txt | insert-sequence |
Layout
hibernate-demo/
├── pom.xml
├── LICENSE
├── scripts/
│ ├── run.sh start one profile, run it, exit
│ ├── run-all.sh regenerate every docs/output/ file
│ └── clean_output.py strip JVM noise + a duplicate SQL echo line from a raw capture
├── src/main/java/com/ankurm/hibernatedemo/
│ ├── HibernateDemoApplication.java
│ ├── model/ Book, WidgetIdentity, WidgetSequence
│ └── scenario/ one CommandLineRunner per profile
└── docs/
├── 00-versions.md .. 03-inserting-objects.md
└── output/*.txt captured, unedited console transcripts
License
MIT — see LICENSE.