NAMED_ENUM failing validation/boot sequence (Hibernate 6.4)

Using Spring Boot 3.2.2 and associated versions (Hibernate 6.4) on Postgres

I have the following java

public enum AddressType{
	HOME,
	WORK,
	SCHOOL;
}

and used in an @Entity class

	@Enumerated(EnumType.STRING)
	@JdbcTypeCode(SqlTypes.NAMED_ENUM)
	private AddressType type;

With the following sql schema

CREATE TYPE address_type AS ENUM('HOME','WORK','SCHOOL');

used in a normal table as

CREATE TABLE IF NOT EXISTS address (
                              id int8 NOT NULL,
                              create_date timestamp NULL,
                              postal_code text NULL,
                              street1 varchar(100) NULL,
                              street2 varchar(100) NULL,
                              country int8 NULL,
                              state int8 NULL,
                              **"type" address_type NULL,**
                              CONSTRAINT address_pkey PRIMARY KEY (id)
);

However, the schema cannot load and validate using this configuration with the following error:

Schema-validation: wrong column type encountered in column [type] in table [address]; found ["address_type" (Types#VARCHAR)], but expecting [type (Types#NAMED_ENUM)]

I cannot find any documentation indicating that there is an alternate way to declare the enumerator. If i keep @Enumerated(EnumType.STRING) and remove the JDBC Type annotation it does validate and boot but I havenā€™t tested to see if that really does what we want (and Iā€™m inclined to believe it does not).

Using @JdbcType(PostgreSQLEnumJdbcType.class). instead also gives me an identical error.

Any help is appreciated.

Well I would love to fix the formatting issue above, but I also get a 422 error on the forums attempting to do so.

Just remove the @JdbcTypeCode annotation. Using named enums is the default on PostgreSQL.

hello I tried this solution but without success Iā€™m still getting the error:

Caused by: jakarta.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [status] in table [table_name]; found [table_name_status (Types#VARCHAR)], but expecting [investmentstatus (Types#NAMED_ENUM)]

only when I use:

hibernate.ddl-auto: validate

When I donā€™t use it, I donā€™t get the error and everything works normally, could there be some adjustment to be made in the dialect?

I use:
org.hibernate.dialect.PostgreSQLDialect

SpringBoot 3.2.1 Hibernate 6.4.1 on Postgres

Then try updating to the latest 6.4 release i.e. 6.4.4.Final

Same error, and my first time here on the hibernate forum, Iā€™ll try to understand how to contribute to this error.

I created a repo to demonstrate the problem in main everything works normally

in the fix/not-work branch and where the problem is, it seems that the problem is using enum with an underlined name.

The repository is not accessible. Either way, if you think you found a bug, please try to create a reproducer with our test case template and if you are able to reproduce the issue, create a bug ticket in our issue tracker and attach that reproducer.

Iā€™m sorry, this is correct:

https://github.com/pedrobachini/pedrobachini-bug-jpa-enum-mapping-postgres

I will provide

https://hibernate.atlassian.net/browse/HHH-17849

1 Like

It was identified that it is not a bug but a new feature and I made the appropriate adjustment.

https://hibernate.atlassian.net/browse/HHH-17855

Worth noting, this got me past validation and then the second i tried to have the enumerators used, it failed.

Iā€™ve unmarked this answer as a solution as the type issue persists. I suspect itā€™s related to the ā€œnot a bug but definitely not well documentedā€ issue of naming schemes, but Iā€™m not sure how to give hibernate any schema that it is happy with and actually uses the types correctly when issuing persist and merge commands.

1 Like

We found a solution:

it would be enough to define the columnDefinition

example:
@Enumerated(EnumType.STRING)
@Column(nullable = false, columnDefinition = ā€œcompose_statusā€)
@JdbcType(PostgreSQLEnumJdbcType.class)
private ComposeStatus status;

1 Like