通过注释使用 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;
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
它应该是
uuid2
:请参阅 5.1.2.2.1。各种附加生成器。
It should be
uuid2
:See 5.1.2.2.1. Various additional generators.
正如 @natan 在评论中指出的那样,如果您使用的是 Hibernate 5,下面的代码就足够了:
使用
BINARY(16)
类型定义id
列MySQL 或其他 SQL 实现中的等效项。As @natan pointed out in a comment, if you are using Hibernate 5, the code below is sufficient:
Define the
id
column with the type ofBINARY(16)
in MySQL or its equivalent in other SQL implementations.HibernateDoc 说您可以使用以下内容:
我希望您使用 Hibernate 3.5。
HibernateDoc says you can use following:
I hope you are using Hibernate 3.5.
尝试...
注意“uuid2”而不是“uuid”。
Try...
Note the "uuid2" as opposed to "uuid".
这将使用 UUID v4,自动生成的 uuid 将像往常一样存储在列中
varchar(36)
:这应该会对性能产生一些影响:
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)
:This should have some performance impact:
BINARY(16)
java.lang.String
instance consumes more memory thanjava.util.UUID
: 112 bytes for UUID as string versus 32 bytes (i.e. two longs + obj header) forUUID
.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
在当前的 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")
代替。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 withjava.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.这是在 Hibernate 5.0.11.FINAL 中使用 uuid 生成器注释的正确方法。
注意:IT 已弃用。
This is the proper way to use annotation for uuid generators in Hibernate 5.0.11.FINAL.
Note: IT is deprecated.