Last updated: 2022-11-28

  1. Learn
  2. Spring Data
  3. UUID Handling

How to handle UUID in MariaDB with Hibernate

By default Hibernate reads and writes UUID as a byte field. In MariaDB, however, this does not exist and instead the char(36) type should be used at the database level. Without further adjustment an error will occur when reading the value from the database:

An attempt was made to get a data value of type 'byte[]' from a data value of type 'CHAR'.

Therefore, the field type should be changed to char(36) and the Hibernate mapping must be defined. This could be done for a single field like this:

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 must 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 MariaDB and many other supported databases. Just start a project directly in the browser to get a runnable configuration.

Start Project
No registration required