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:
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:
spring.jpa.hibernate.ddl-auto = update
run the app
database is updated
stop the app
spring.jpa.hibernate.ddl-auto = validate
run the app
exception is thrown
Am I doing something wrong? Or is it really a bug?
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
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.
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.
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: https://www.h2database.com/html/datatypes.html#enum_type