通过注释使用Hibernate UUIDGenerator

我正在使用我的uuid如下:

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

但我得到一个聪明的hibernate警告:

使用org.hibernate.id.UUIDHexGenerator,它不生成符合IETF RFC 4122标准的UUID值; 请考虑使用org.hibernate.id.UUIDGenerator

所以我想切换到org.hibernate.id.UUIDGenerator ,现在我的问题是如何告诉它到Hibernate的生成器。 我看到一些人用它作为“hibernate-uuid” – 所以这就是我所尝试的,但有负面的结果:

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

应该是uuid2

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

见5.1.2.2.1。 各种其他发电机 。

HibernateDoc说你可以使用下面的代码:

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

我希望你使用Hibernate 3.5。

尝试…

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

注意“uuid2”而不是“uuid”。

未知的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; }