Issue with Postgres, @MappedSuperclass, generics and enums

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 @Entity subclass 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 @MappedSuperclass is not parameterized
  • The enum field is declared on the @Entity class directly instead of the @MappedSuperclass one

The bug still occurs when:

  • Using the pgjdbc-ng Postgres driver instead of the standard one
  • The @Entity does 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?

Sorry, no idea, but thanks for creating the reproducer. Please add the link to the JIRA issue and we will look into it ASAP.

Here it is (finally): [HHH-15970] - Hibernate JIRA

1 Like