Hibernate DDL validation fails on enum database column type

Description

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.

After switching to Hibernate ORM 6.2.1 this table column was converted to MySQL’s enum data type.
When I try to start my app with spring.jpa.hibernate.ddl-auto set to validate - an exception is thrown:

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


Hibernate Discourse post:

Activity

Show:

Gavin King June 30, 2023 at 6:45 PM

Thanks for the fix!

Čedomir Igaly May 10, 2023 at 2:49 PM

Fix for 6.2 - pull request 6541

Gavin King May 8, 2023 at 6:34 PM

Ah, nice catch, this was my fault.

Čedomir Igaly April 26, 2023 at 11:26 AM
Edited

MySQL is treating enum column type as SQL type 1 (CHAR). When schema is validated method org.hibernate.dialect.Dialect#equivalentTypes is returning false because it is using org.hibernate.type.SqlTypes#isVarcharType which is returning false for 1 (CHAR).

 

Suggested fix is to use less strict method org.hibernate.type.SqlTypes#isCharacterType which will accept VARCHAR and CHAR as equivalent types. Alternatively, use of isCharacterType instead of isVarcharType can be pushed down to MySqlDialect.

 

Suggested fix is in pull request 6462 pull request 6527

Fixed

Details

Assignee

Reporter

Components

Fix versions

Affects versions

Priority

Created April 21, 2023 at 8:56 AM
Updated April 30, 2024 at 8:13 PM
Resolved May 10, 2023 at 8:07 AM