Hi,
I had a very weird and specific issue today using a parameterized @MappedSuperclass containing an enum field with a Postgres datasource. I’m using the latest Hibernate ORM 6.1.6 with Spring Boot 3.0.
Here’s how it’s triggered:
- Use a Postgresql data source
- Have a parameterized
@MappedSuperclass class ParameterizedParent<T> { ... } - Have an enum type field on this
@MappedSuperclass(something like@Enumerated(EnumType.STRING) private MyEnum myEnum;) - Have a regular
@Entitysubclass of the parameterized@MappedSuperclass - Generate the DDL (hibernate.hbm2ddl.auto=create/create-drop/update)
The bug does NOT occur when:
- Using another database like H2 or HSQL (I did not test others)
- The
@MappedSuperclassis not parameterized - The enum field is declared on the
@Entityclass directly instead of the@MappedSuperclassone
The bug still occurs when:
- Using the pgjdbc-ng Postgres driver instead of the standard one
- The
@Entitydoes not declare the type parameter (raw usage of generics) on its parent class - The enum field is not annotated or annotated with
@Enumerated(EnumType.ORDINAL)=> the exception become “mapped as 5 but is -3”
A simple workaround is to set the column definition explicitly on the enum field with @Column(columnDefinition = "varchar(255)") (or similar declaration if the enum should be saved as ordinal).
The whole thing is described in this repo, with a bunch of unit tests for each case above and the full exception I get.
Before I open a bug, does anyone have an idea of what went wrong here, and how it could be fixed?