[英]Using Hibernate UUIDGenerator via annotations
I'm using my uuid as following: 我使用我的uuid如下:
@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: 但是我收到了一个聪明的Hibernate警告:
Using org.hibernate.id.UUIDHexGenerator which does not generate IETF RFC 4122 compliant UUID values;
使用org.hibernate.id.UUIDHexGenerator不会生成符合IETF RFC 4122的UUID值; consider using org.hibernate.id.UUIDGenerator instead
考虑改用org.hibernate.id.UUIDGenerator
So I want to switch to org.hibernate.id.UUIDGenerator
, now my question is how should I tell it to Hibernate's generator. 所以我想切换到
org.hibernate.id.UUIDGenerator
,现在我的问题是如何将它告诉Hibernate的生成器。 I saw some guy used it as a "hibernate-uuid" - so this is what I've tried, but with negative result: 我看到有人用它作为“休眠uuid”-这是我尝试过的方法,但结果是负面的:
@Id
@GeneratedValue(generator = "hibernate-uuid")
@GenericGenerator(name = "hibernate-uuid", strategy = "hibernate-uuid")
@Column(name = "uuid", unique = true)
private String uuid;
It should be uuid2
: 应该是
uuid2
:
...
@GenericGenerator(name = "uuid", strategy = "uuid2")
...
See 5.1.2.2.1. 参见5.1.2.2.1。 Various additional generators .
各种附加发电机 。
HibernateDoc says you can use following: HibernateDoc说您可以使用以下内容:
@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. 我希望您正在使用Hibernate 3.5。
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". 注意“ uuid2”而不是“ uuid”。
As @natan pointed out in a comment, if you are using Hibernate 5 the below code is sufficient: 正如@natan在评论中指出的那样,如果您使用的是Hibernate 5,那么下面的代码就足够了:
@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. 在MySQL中定义
id
列的类型为BINARY(16)
或在其他SQL实现中等效。
Unknown Id.generator: hibernate-uuid
未知的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;
}
This will use UUID v4 and the auto generated uuid will be stored in the column as usual varchar(36)
: 这将使用UUID v4,并且自动生成的uuid将像往常一样存储在列
varchar(36)
:
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
@Column(length = 36)
private String uuid;
This should have some performance impact: 这应该会对性能产生一些影响:
BINARY(16)
BINARY(16)
java.lang.String
instance consumes more memory than java.util.UUID
: 112 bytes for UUID as string versus 32 bytes (ie two longs + obj header) for UUID
. java.lang.String
实例消耗比多个存储器java.util.UUID
:112个字节用于UUID作为字符串对32个字节(即,两个多头+ OBJ报头)为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. 但是,使用带字符串的UUID会更容易-编写查询更容易,并且您可以看到表的内容。
Tested on Hibernate 5.3 在Hibernate 5.3上测试
With current 5.4.2 Hibernate version, 在当前的5.4.2 Hibernate版本中,
if you want a Human-Readable varchar(36) field in the database table, 如果要在数据库表中使用人类可读的 varchar(36)字段,
but also a Serializable UUID data type in your Java Class, 而且您的Java类中还有一个可序列化的 UUID数据类型,
you can use @Type(type = "uuid-char")
at the same time you declare your field member with java.util.UUID
type. 您可以在使用
java.util.UUID
类型声明字段成员的同时使用@Type(type = "uuid-char")
。
Note that @Column(length = 36)
is important to reduce from 255 to 36 the field length in MySQL. 注意,
@Column(length = 36)
对于将MySQL中的字段长度从255减少到36很重要。
Note that with PostgreSQL you should use @Type(type = "pg-uuid")
instead. 请注意,在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;
@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. 这是在Hibernate 5.0.11.FINAL中对uuid生成器使用批注的正确方法。
Note: IT is deprecated. 注意:不建议使用IT。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.