# UUID field of type "text"

**URL:** <https://discourse.hibernate.org/t/uuid-field-of-type-text/10597>\
**Category:** Hibernate ORM\
**Created:** [November 11, 2024, 10:50pm UTC](https://discourse.hibernate.org/t/uuid-field-of-type-text/10597 "2024-11-11T22:50:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Calcote](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/john_calcote/32/3595_2.png) [@John\_Calcote](https://discourse.hibernate.org/u/John_Calcote)\
**Post date:** [November 11, 2024, 10:50pm UTC](https://discourse.hibernate.org/t/uuid-field-of-type-text/10597/1 "2024-11-11T22:50:58Z")

</div>

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:

```auto
                                            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:

```auto
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).

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 12, 2024, 10:09am UTC](https://discourse.hibernate.org/t/uuid-field-of-type-text/10597/2 "2024-11-12T10:09:16Z")

</div>

Did you read the [migration guide](https://github.com/hibernate/hibernate-orm/blob/6.0/migration-guide.adoc#uuid-mapping-changes)? 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)`
