Hello everyone,
I’m trying to understand what the motivation was in Hibernate 6.2(or 6.3) to map enums annotated with “@Enumerated(STRING)” to ENUM data types only for MySQL. The migration guide only mentions that “On MySQL, enums are now stored using the ENUM datatype by default”, but not why.
We now have to search around 50 Spring projects and apparently we have the following options:
- Switch off validation, which is a pity as it is a very useful tool.
- Add a column annotation to the enum properties in the entities that sets the value columnDefinition = “varchar”. Which is the easiest, but the reason is not obvious at first glance.
- In all projects, a migration for each enum column that was previously a VARCHAR, including the addition of the enum values. A lot of effort for something that used to run smoothly.
As a software developer, I currently only see problems but no advantage. Apparently it is no longer enough to manage the enums in the code, I have to add a more or less complex migration (e.g. an enum rename) for each enum change.
Can someone enlighten me?
Thank you very much
Schema validation was adapted to allow either varchar
or enum
when enum
is expected. Also see [HHH-17908] - Hibernate JIRA
You simply have to update the Hibernate ORM version to at least 6.4.5.
Using an enum type on the database side usually has the benefit that the query optimizer can do smarter decisions. It’s also way nicer to store the enum values in the type definition, because that way, you can actually really validate if the schema is still correct. If you rename an enum value and just use varchar, schema validation wouldn’t have noticed this before. Now it finally notices and will save you from runtime errors that an enum with some name was not found.
MySQL was just the first database for which this was implemented. We also added support for H2 in 6.5 I think, and for PostgreSQL you have to opt-in because the enum type is not compatible on the JDBC side with either ordinal or varchar mappings.
ORM 6.6 will also support enums for Oracle and we might even add support for more databases. With ORM 7 we might even change the mappings for Oracle and PostgreSQL to default to the native types, with an option to switch to the old mappings. Also see [HHH-17905] - Hibernate JIRA
Hey thanks for the quick and detailed explanation and for the reference to HHH-17908.
I understand it better now but for me it is enough if the software manages the enums and not the database. But that’s just my opinion.
Basically I would be in favour of allowing new additions but keeping existing ones working. In this case perhaps via an @Enumerated(ENUM).
Best regards
Your existing database is still going to work fine and once you update to Hibernate ORM 6.4.5+, schema validation will also stop complaining.
Using the implicit ENUM type on MySQL and H2 is also not much different from using a check constraint which we generate since a while now as well, so I don’t see any need for change for your particular case. You just need to update.