通过注释使用休眠 UUID 生成器

2022-08-31 12:06:35

我使用我的uuid如下:

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

但是我得到了一个智能休眠警告:

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

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

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

答案 1

它应该是:uuid2

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

请参阅 5.1.2.2.1。各种附加发电机


答案 2

HibernateDoc说你可以使用以下内容:

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

我希望你正在使用Hibernate 3.5。