Hibernate validate fails on enum

Few days ago I switched my Java Spring Boot project from Hibernate ORM 6.1.7 to Hibernate ORM 6.2.1. The underlying database is MySQL 8 which supports enum data type.

In one of my entities I have enum which is annotated by @Enumerated(EnumType.STRING) and so far it was working as expected - varchar table column was created by Hibernate few years ago.

spring.jpa.hibernate.ddl-auto = validate

In project configuration I always use validate just to be sure if and when any changes appear so I could apply those changes to my production database before deploy.

After switching to Hibernate ORM 6.2.1 this table column was converted to MySQL’s enum data type which is okay with me. I just have to upgrade my production database with:

ALTER TABLE `custom_currency` MODIFY COLUMN `sign_position` enum(
    'AFTER_NO_SPACE', 'AFTER_WITH_SPACE', 'BEFORE_NO_SPACE', 'BEFORE_WITH_SPACE'
) COLLATE utf8mb4_unicode_ci DEFAULT NULL;

When I try to start my app with spring.jpa.hibernate.ddl-auto set to validate - exception is thrown:

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException:
Schema-validation: wrong column type encountered in column [sign_position]
in table [custom_currency];
found [enum (Types#CHAR)], but expecting [varchar(255) (Types#VARCHAR)]

If I change ddl-auto setting to update or none, the table column of enum type stays as it is and application can boot up.

Steps are:

  1. spring.jpa.hibernate.ddl-auto = update
  2. run the app
  3. database is updated
  4. stop the app
  5. spring.jpa.hibernate.ddl-auto = validate
  6. run the app
  7. exception is thrown

Am I doing something wrong? Or is it really a bug?

Looks like a bug. Please create a JIRA issue for this.

The bug is created. Link: [HHH-16498] - Hibernate JIRA

1 Like

I can reproduce the problem and have some idea(s) (but no time to analyze it more today) why it is not working. Only it is failing under 6.1.7 as well. Did you’ve changed version of MySQL driver while switching from 6.1.7 to 6.2.1?

With 6.1.7 I didn’t have any enum database columns. This one column I mentioned changed automatically at the moment when auto-ddl was set to ‘update’ because of some new things I did to my model, not directly related to this enum.

I can make changes in my development environment if you need test case.

No need for test case, it can be very simple - ID and enum columns. More difficult has been to add that to other test cases :slight_smile:
Cause of the problem is that MySQL is returning 1 (CHAR) as SQL type for custom enum column type. When schema is validated Dialect is not accepting CHAR and VARCHAR as equivalent types. My suggested fix is to use less strict check which will accept CHAR as equivalent to VARCHAR.

1 Like

I had similar issue, when using @Enumerated(EnumType.ORDINAL)

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException:
Schema-validation: wrong column type encountered in column [status]
in table [discount];
found [enum (Types#INT)], but expecting [varchar(255) (Types#SMALLINT)]

I could fix/workaround by adding

@Column(columnDefinition = "INT")

to the enum column in the entity class.

In your case, add @Column(columnDefinition = "CHAR") to your column.

1 Like