86

I'm using my uuid as following:

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
@Column(name = "uuid", unique = true)
private String uuid;

but I'm getting a smart Hibernate warning:

Using org.hibernate.id.UUIDHexGenerator which does not generate IETF RFC 4122 compliant UUID values; consider using org.hibernate.id.UUIDGenerator instead

So I want to switch to org.hibernate.id.UUIDGenerator, now my question is how should I tell it to Hibernate's generator. I saw some guy used it as a "hibernate-uuid" - so this is what I've tried, but with negative result:

@Id
@GeneratedValue(generator = "hibernate-uuid")
@GenericGenerator(name = "hibernate-uuid", strategy = "hibernate-uuid")
@Column(name = "uuid", unique = true)
private String uuid;
Soumitri Pattnaik
  • 3,246
  • 4
  • 24
  • 42
Martin
  • 1,460
  • 1
  • 13
  • 17

8 Answers8

115

It should be uuid2:

...
@GenericGenerator(name = "uuid", strategy = "uuid2")
...

See 5.1.2.2.1. Various additional generators.

axtavt
  • 239,438
  • 41
  • 511
  • 482
  • 4
    [It's the same for 4.1](http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#mapping-declaration-id). Also, note that it uses java.util.UUID.randomUUID(). – CorayThan May 01 '13 at 18:15
  • 28
    A simple `@Id @GeneratedValue private java.util.UUID id;` works in Hibernate 5+. See [documentation](http://docs.jboss.org/hibernate/orm/5.0/userguide/html_single/Hibernate_User_Guide.html#identifiers). – Natan Cox Jun 10 '16 at 09:11
  • 1
    what is the difference between `strategy = "uuid"` and `strategy = "uuid2"` ? – Olivier Boissé Jul 04 '20 at 10:07
  • @OlivierBoissé- this should help https://stackoverflow.com/questions/25218261/hibernate-uuid-hex-vs-uuid2 – Vipul Jain Aug 08 '21 at 19:18
22

HibernateDoc says you can use following:

@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
@Column(name = "uuid", unique = true)
private String uuid;

I hope you are using Hibernate 3.5.

CSchulz
  • 10,882
  • 11
  • 60
  • 114
21

As @natan pointed out in a comment, if you are using Hibernate 5 the below code is sufficient:

@Id 
@GeneratedValue
private java.util.UUID id;

Define the id column with the type of BINARY(16) in MySQL or it's equivalent in other SQL implementations.

senjin.hajrulahovic
  • 2,961
  • 2
  • 17
  • 32
  • 1
    or you can also add `@Type(type="uuid-char")` annotation to use `VARCHAR(36)` column – Sergey Ponomarev Mar 25 '19 at 14:29
  • For postgres @Type(type="pg-uuid") :D – Akif Hadziabdic Apr 06 '21 at 18:45
  • @senjin.hajrulahovic I am using Hibernate 5.6.15.final. When I try to insert the table with this configuration, throw: cause by: java.sql.sqlexception: field 'id' doesn't have a default value and the execution stops. The DDL that generates me is: Create table `regions` ( `ID` Binary (255) Not Null, `Name` Varchar (255) not null, Primary Key (`ID`) ) Engine = Innodb Default CHARSET = UTF8Collate = UTF8MB4_0900_AI_CI Everything seems correct in the database, which leads me to believe that Hibernate is not automatically generating the ID field by inserting the data into the table – Toms_Hd3z Jun 09 '23 at 15:21
11

Try...

@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(name = "uuid", columnDefinition = "BINARY(16)")
public UUID getId()
{
    return id;
}

public void setId(UUID i)
{
    id = i;
}

Note the "uuid2" as opposed to "uuid".

kervin
  • 11,672
  • 5
  • 42
  • 59
5

This will use UUID v4 and the auto generated uuid will be stored in the column as usual varchar(36):

@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(length = 36)
private String uuid;

This should have some performance impact:

  • consumed size is more than BINARY(16)
  • after hydration the java.lang.String instance consumes more memory than java.util.UUID: 112 bytes for UUID as string versus 32 bytes (i.e. two longs + obj header) for UUID.

But it's much more easier to work with string'ed UUID - easier to write queries and you can see the contents of the table.

Tested on Hibernate 5.3

Sergey Ponomarev
  • 2,947
  • 1
  • 33
  • 43
4

Unknown Id.generator: hibernate-uuid

@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "id", unique = true)
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}
Ahmad R. Nazemi
  • 775
  • 10
  • 26
3

With current 5.4.2 Hibernate version,

if you want a Human-Readable varchar(36) field in the database table,
but also a Serializable UUID data type in your Java Class,
you can use @Type(type = "uuid-char") at the same time you declare your field member with java.util.UUID type.

Note that @Column(length = 36) is important to reduce from 255 to 36 the field length in MySQL.

Note that with PostgreSQL you should use @Type(type = "pg-uuid") instead.

import org.hibernate.annotations.Type
import java.util.UUID
import javax.persistence.Column
import javax.persistence.GeneratedValue
import javax.persistence.Id

@Id @GeneratedValue
@Type(type = "uuid-char") @Column(length = 36)
private UUID id;
JavierFuentes
  • 1,840
  • 18
  • 13
0
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
@Column(name = "UUID_ID")
public String getId(){
return id;
}

This is the proper way to use annotation for uuid generators in Hibernate 5.0.11.FINAL.

Note: IT is deprecated.

ndsmyter
  • 6,535
  • 3
  • 22
  • 37