Last updated: 2024-05-24
How to handle UUID in MySQL with Hibernate
By default Hibernate reads and writes UUID as a byte field. In MySQL, however, this is not recommended and instead the char(36)
type should be used at the database level.
In a previous version of this article the adjustment was made for MariaDB. However since version 10.7
MariaDB has a native UUID type which works very well with Hibernate, so this setup isn't required anymore.
To use the character type for a single field in MySQL the following annotations can be used:
Preparing a UUID field in a JPA / Hibernate Entity
In older Hibernate versions before Spring Boot 3, @Type(type="uuid-char")
could be used instead of @JdbcTypeCode(SqlTypes.CHAR)
. Alternatively, the type can also be set for the entire Hibernate / Spring Boot application - for this purpose, the following Hibernate property could be added. With an annotation you could still overwrite this setting.
In Bootify's Free plan, a Spring Boot application can be initialized with any database schema. UUID types are configured automatically - for MySQL, MariaDB and many other supported databases. Just start a project directly in the browser to get a runnable configuration.
Start Project
No registration required