Files
hibernate-demo/enums/README.md
T

39 lines
2.5 KiB
Markdown

# enums
Companion module for the ankurm.com article **Enum Mapping in Hibernate 7: The ORDINAL Trap, STRING, PostgreSQL Native Enums and Converters**.
Every SQL statement, error message and row 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 and on a local PostgreSQL 18: no Spring, no Docker needed.
## Versions
| Component | Version | Notes |
|---|---|---|
| Hibernate ORM | `7.4.17.Final` | newest 7.x GA in `maven-metadata.xml` on 2026-11-19 (`8.0.0.CR1` is listed after it but is not GA) |
| Jakarta Persistence | `3.2.0` | `@EnumeratedValue` is new in this version |
| H2 | `2.4.240` | in memory |
| PostgreSQL server / driver | `18.6` / `42.7.14` | server from the zonky embedded binaries, see `scripts/start-postgres.sh` |
| JUnit / AssertJ | `6.0.3` / `3.27.7` | |
| Java | `25` (Temurin 25.0.5.1) | |
## Run
```bash
./scripts/start-postgres.sh # throwaway PostgreSQL 18.6 on 127.0.0.1:55432 (skip if you have your own: -Dpg.host / -Dpg.port)
./scripts/run-all.sh # or: mvn clean test
```
## Tests and what they write
| Test | Output | Question it answers |
|---|---|---|
| `T01_DdlTest` | `output/01-ddl.txt` | Which column type and check constraint does each mapping make Hibernate create, on H2 and PostgreSQL? |
| `T02_ReleaseTest` | `output/02-release-two.txt` | Release 1 wrote NEW/PAID/SHIPPED; release 2 inserts, appends or renames a constant. What does it read, and can it write the new value? |
| `T03_NativeEnumTest` | `output/03-native-enum.txt` | PostgreSQL native enum: ordering, `ALTER TYPE ... ADD / RENAME / DROP VALUE`, the in-transaction trap |
| `T03_NativeEnumTest` | `output/04-prefer-native.txt` | What does `hibernate.type.prefer_native_enum_types` change for plain `@Enumerated(STRING)`? |
| `T04_UpdateTest` | `output/05-hbm2ddl-update.txt` | Does `hbm2ddl.auto=update` fix the schema after a constant is added? |
| `T05_FixTest` | `output/06-migrations.txt` | The hand-written migration each change needs, and release 2 afterwards |
| `T06_QueryTest` | `output/07-queries.txt` | SQL sent for HQL on enums, `ORDER BY` per mapping, and a bad value in the column |
| `T07_PreferNativeAssertTest` | (assertions only) | `prefer_native_enum_types` really switches PostgreSQL to a native type |
[`Variants`](src/main/java/com/ankurm/enums/Variants.java) holds every mapping as a small entity over the same table `orders(id, status)`. "V1" is release 1; "Mid" / "App" / "Ren" are release 2 with a constant inserted in the middle, appended, or renamed.