通过注释使用 Hibernate UUIDGenerator

发布于 2024-11-15 13:14:33 字数 714 浏览 6 评论 0原文

我使用我的 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;

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

玻璃人 2024-11-22 13:14:33

它应该是 uuid2

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

请参阅 5.1.2.2.1。各种附加生成器

It should be uuid2:

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

See 5.1.2.2.1. Various additional generators.

沒落の蓅哖 2024-11-22 13:14:33

正如 @natan 在评论中指出的那样,如果您使用的是 Hibernate 5,下面的代码就足够了:

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

使用 BINARY(16) 类型定义 id 列MySQL 或其他 SQL 实现中的等效项。

As @natan pointed out in a comment, if you are using Hibernate 5, the code below is sufficient:

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

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

时光礼记 2024-11-22 13:14:33

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

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

我希望您使用 Hibernate 3.5。

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.

岛徒 2024-11-22 13:14:33

尝试...

@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”。

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

凉风有信 2024-11-22 13:14:33

这将使用 UUID v4,自动生成的 uuid 将像往常一样存储在列中 varchar(36)

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

这应该会对性能产生一些影响:

  • 消耗的大小大于 BINARY(16)< /code>
  • 水合后,java.lang.String 实例比 java.util.UUID 消耗更多内存:UUID 作为字符串为 112 字节,而 32 字节(即两个longs + obj 标头)用于 UUID

但使用字符串 UUID 更容易 - 更容易编写查询,并且您可以看到表的内容。

在 Hibernate 5.3 上测试

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

相权↑美人 2024-11-22 13:14:33

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

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;
}
分开我的手 2024-11-22 13:14:33

在当前的 5.4.2 Hibernate 版本中,

如果您希望数据库表中有一个人类可读 varchar(36) 字段,
而且也是 Java 类中的可序列化 UUID 数据类型,
您可以使用@Type(type = "uuid-char")同时使用java.util.UUID声明您的字段成员类型。

请注意,@Column(length = 36) 对于将 MySQL 中的字段长度从 255 减少到 36 非常重要。

请注意,对于 PostgreSQL,您应该使用 @Type(type = "pg-uuid") 代替。

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;

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;
〗斷ホ乔殘χμё〖 2024-11-22 13:14:33
@Id
@GeneratedValue(generator = "uuid")
@GenericGenerator(name = "uuid", strategy = "uuid")
@Column(name = "UUID_ID")
public String getId(){
return id;
}

这是在 Hibernate 5.0.11.FINAL 中使用 uuid 生成器注释的正确方法。

注意:IT 已弃用。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文