Using Hibernate UUIDGenerator via annotations

JavaHibernateAnnotationsUuid

Java Problem Overview


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;

Java Solutions


Solution 1 - Java

It should be uuid2:

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

See 5.1.2.2.1. Various additional generators.

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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".

Solution 5 - Java

> 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;
}

Solution 6 - Java

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

Solution 7 - Java

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;

Solution 8 - Java

@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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartinView Question on Stackoverflow
Solution 1 - JavaaxtavtView Answer on Stackoverflow
Solution 2 - JavaCSchulzView Answer on Stackoverflow
Solution 3 - Javasenjin.hajrulahovicView Answer on Stackoverflow
Solution 4 - JavakervinView Answer on Stackoverflow
Solution 5 - JavaAhmad R. NazemiView Answer on Stackoverflow
Solution 6 - JavaSergey PonomarevView Answer on Stackoverflow
Solution 7 - JavaJavierFuentesView Answer on Stackoverflow
Solution 8 - JavaSumitView Answer on Stackoverflow