UUID field of type "text"

I have a postgresql-backed hibernate 5 database that I’m converting to hibernate 6.6. I have various uuid fields throughout my table set that are defined as text fields:

                                            Table "public.emailnotification"
        Column        |          Type           | Collation | Nullable | Default | Storage  | Stats target | Description
----------------------+-------------------------+-----------+----------+---------+----------+--------------+-------------
 uuid                 | character varying(255)  |           | not null |         | extended |              |
 failurecount         | integer                 |           |          |         | plain    |              |
 headline             | character varying(512)  |           |          |         | extended |              |
 notificationcontents | character varying(1024) |           |          |         | extended |              |
Indexes:
    "emailnotification_pkey" PRIMARY KEY, btree (uuid)
Referenced by:
    TABLE "emailnotification_attachment" CONSTRAINT "fkg2jd3ppvymlgu0tdb29g8tw1i" FOREIGN KEY (emailnotification_uuid) REFERENCES emailnotification(uuid)
    TABLE "emailnotification_recipientemails" CONSTRAINT "fkgi7grlskjjj1b30dsk72cjr5c" FOREIGN KEY (emailnotification_uuid) REFERENCES emailnotification(uuid)
Access method: heap

In my Java code, I have the field defined as a UUID and I keep getting this error:

2024-11-11 22:37:31,028 [main] (AbstractEntityManagerFactoryBean.java:426) ERROR LocalContainerEntityManagerFactoryBean [] - Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [uuid] in table [EmailNotification]; found [varchar (Types#VARCHAR)], but expecting [char(36) (Types#CHAR)]

I’ve tried using a custom converter, I’ve tried specifying the file type with @JdbcTypeCode(Typed.VARCHAR), I’ve tried using hibernate’s @Type annotation. I almost tried a custom dialect, but that just seemed so overkill. Is there not a way to make hibernate happy without entirely disabling schema validation?

I jumped into the debugger and found the point in hibernate where the exception was getting thrown. Many UUID fields go through that code - they all come in as VARCHAR (12) and are compared with VARCHAR and pass. Only this field (which is used as a primary key in this table) comes in as UUID(3000).

Did you read the migration guide? You can change the default SQL type to use for UUID globally to ease migration.

The validation error is a real “issue” though, you’re just using the wrong data type. Hibernate ORM expects text UUIDs to be 36 chars exactly. You should be able to alter table emailnotification alter column set data type char(36)