Spring Data JPA Postgres UUID @Id

I read lots of outdated info on this matter.

Using PostgreSQL v13+ there is a function called gen_random_uuid().
See the manual page of Postgres UUID.

Here’s an example entity called Page

As you see you can append the columnDefinition to the @Column annotation. “UUID default gen_random_uuid()”
This translates to following SQL

And you don’t have to worry about having an empty ID when inserting records.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.