Using Hibernate UUIDGenerator via annotations
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.
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.
It should be uuid2
:
...
@GenericGenerator(name = "uuid", strategy = "uuid2")
...
See 5.1.2.2.1. Various additional generators.