Unable to generate UUID id for my entities

11,016

Solution 1

I use the following configuration and it works. I am using Glassfish as application server.

@Id
@GenericGenerator(name = "uuid", strategy = "uuid2")
@GeneratedValue(generator = "uuid")
@Column(name = "key", unique = true, nullable = false)
@Type(type="pg-uuid")
private UUID key;

The table key has the Postgres uuid type:

CREATE TABLE billuser.billingresult (key uuid NOT NULL,   .... )...

Solution 2

I used this configuration and this is working for me and I am using

  • Spring Boot 1.5.2
  • Hibernate 5
  • PostgreSQL 9.6

Entity.java

@Id  
@Column(updatable = false, nullable = false, columnDefinition = "uuid DEFAULT uuid_generate_v4()")
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;

before that I am suggesting you to load uuid-ossp it into your database to use it.

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
Share:
11,016
davioooh
Author by

davioooh

Hi, I'm David Castelletti. I like to create things with Java & Kotlin (❤). LinkedIn profile Personal Page + Blog (italian)

Updated on July 25, 2022

Comments

  • davioooh
    davioooh almost 2 years

    I'm working on a web project using:

    • Spring 4.x (Spring Boot 1.3.3)
    • Hibernate 4.x
    • PostgreSQL 9.x

    I'm new to Postgres DB and for my tables I decided to use (for the first time) UUID identifiers, but I'm having some troubles...

    For ID field I used Postgres uuid type and I set as default value uuid_generate_v4(). All works correctly when I generate a new row directly by a PSQL insert, but I cannot create a new record by my application.

    As an example, this is how I declared an entity in my application:

    @Entity
    @Table(name = "users")
    public class User {
    
        @Id
        @Type(type = "pg-uuid")
        private UUID id;
    
        // other fields and methods...
    
    }
    

    For this declaration I'm using the Type Hibernate annotation.

    Find operations work well, but when I try to make an insert I get this exception:

    org.springframework.orm.jpa.JpaSystemException: ids for this class must be manually assigned before calling save(): net.myapp.User;

    Followiong this tutorial I tried to solve the issue. I changed my entity definition to:

    @Entity
    @Table(name = "users")
    public class User {
    
        @GeneratedValue(generator = "uuid2")
        @GenericGenerator(name = "uuid2", strategy = "uuid2")
        @Column(columnDefinition = "BINARY(16)")
        @Id
        private UUID id;
    
        // other fields and methods...
    }
    

    But now I'm getting wrong ID values when I retrieve (existing) data from DB (still haven't tried insert...).

    So what is the right way to define ID field in my case?


    UPDATE

    Using the second definition...

    When I try to find by ID, I'm getting:

    org.postgresql.util.PSQLException: ERROR: operator does not exist: uuid = bytea Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.

    When I try to create new record:

    org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a];