hibernateuuidgrails-4

Grails using uuid id generator: Not very random


A simple Grails 4 app uses the uuid id generator with the Hibernate 7.0.4 plugin and PostgreSQL 10 under Ubuntu 20.04. Here is some output after 4 instances have been created:

=> select id,ttag,name from msg_template;
                id                | ttag |      name       
----------------------------------+------+-----------------
 ff808181781632030178163215190000 | publ | Notice01
 ff8081817816320301781632151f0001 | publ | Notice02
 ff808181781632030178163215230002 | publ | Signing01
 ff8081817816320301781635161b0003 | acme | Some thingy!

My assumption was uuid = randomness, but this result is very un-random. Is randomness not to be expected?


Solution

  • UUIDs come in different versions and variants, see https://en.wikipedia.org/wiki/Universally_unique_identifier. Not every version generates random values.

    However, version 4 generates random values based on a random number generator.

    For Java, the random number generator can be defined more precisely via the system property java.security.egd, see https://www.baeldung.com/java-security-egd.

    With java.util.UUID.randomUUID() you generate UUIDs of version 4.

    For Hibernate there are different ways to use UUID, here

    can help.