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(
) 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

I’m getting the same error and tried fo fix it by modifying the column type to enum, as suggest. However, with H2, I’m getting

Schema-validation: wrong column type encountered in column [match_type] in table [potential_match]; found [enum (Types#OTHER)], but expecting [varchar(255) (Types#VARCHAR)]
	at org.hibernate.tool.schema.internal.AbstractSchemaValidator.validateColumnType(AbstractSchemaValidator.java:165)
	at org.hibernate.tool.schema.internal.AbstractSchemaValidator.validateTable(AbstractSchemaValidator.java:152)
	at org.hibernate.tool.schema.internal.GroupedSchemaValidatorImpl.validateTables(GroupedSchemaValidatorImpl.java:46)
	at org.hibernate.tool.schema.internal.AbstractSchemaValidator.performValidation(AbstractSchemaValidator.java:97)
	at org.hibernate.tool.schema.internal.AbstractSchemaValidator.doValidation(AbstractSchemaValidator.java:75)
	at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:295)
	at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.lambda$process$5(SchemaManagementToolCoordinator.java:145)

The problem is in org.hibernate.dialect.H2Dialect which does not override public String getEnumTypeDeclaration(String name, String[] values) even though H2 now supports enum types:

Hibernate ORM doesn’t yet support the enum type on H2. If you want to add support for that, please consider opening a PR against the main branch with the fix and a test. You can use hibernate-orm/hibernate-core/src/test/java/org/hibernate/orm/test/schemavalidation/MySqlExistingEnumColumnValidationTest.java at d4accd023317cb429964cb109c0b4162d1ee2675 · hibernate/hibernate-orm · GitHub as inspiration for a test.

Hi @beikov,

I’d like to give it a try. Can you please create a JIRA issue which I can use as a reference in my test and commit messages?

You can create a Jira issue yourself, you just have to create an Atlassian account :wink:

Ok, I’ve tried to be lazy … Please see [HHH-17675] - Hibernate JIRA

1 Like