Using Hibernate UUIDGenerator via Annotations

I use my uuid as follows:

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

but I get a hibernate smart warning:

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

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

 @Id @GeneratedValue(generator = "hibernate-uuid") @GenericGenerator(name = "hibernate-uuid", strategy = "hibernate-uuid") @Column(name = "uuid", unique = true) private String uuid; 
+63
java uuid annotations hibernate
Jun 15 '11 at 11:14
source share
7 answers

It should be uuid2 :

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

See 5.1.2.2.1. Various additional generators .

+91
Jun 15 '11 at 11:22
source share

HibernateDoc says you can use the following:

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

Hope you are using Hibernate 3.5.

+15
Jun 15 '11 at 11:21
source share

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

Pay attention to uuid2, not uuid.

+10
Apr 30 '15 at 4:59
source share

Unknown id: 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; } 
+3
01 Sep '16 at 8:23
source share

As @natan noted in the comment, if you are using Hibernate 5, then the code below is enough:

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

Define an id column of type BINARY(16) in MySQL or its equivalent in other SQL implementations.

+3
Sep 11 '18 at 14:45
source share
 @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid") @Column(name = "UUID_ID") public String getId(){ return id; } 

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

Note: IT is deprecated.

0
Apr 19 '18 at 10:54
source share

In this case, UUID v4 will be used, and the automatically generated uuid will be saved in the column as a regular varchar(36) :

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

This should have some impact on performance: * the consumed size is larger than BINARY(16) * after hydration, the java.lang.String instance consumes more memory than java.util.UUID

But it’s much easier to work with string UUIDs - it’s easier to write queries, and you can see the contents of the table.

Tested on Hibernate 5.3

0
Feb 04 '19 at 1:06
source share



All Articles