Java 通过注解使用 Hibernate UUIDGenerator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6356834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:17:15  来源:igfitidea点击:

Using Hibernate UUIDGenerator via annotations

javahibernateannotationsuuid

提问by Martin

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:

但我收到了一个智能的休眠警告:

Using org.hibernate.id.UUIDHexGenerator which does not generate IETF RFC 4122 compliant UUID values; consider using org.hibernate.id.UUIDGenerator instead

使用不生成符合 IETF RFC 4122 的 UUID 值的 org.hibernate.id.UUIDHexGenerator;考虑使用 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. I saw some guy used it as a "hibernate-uuid" - so this is what I've tried, but with negative result:

所以我想切换到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;

采纳答案by axtavt

It should be uuid2:

它应该是uuid2

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

See 5.1.2.2.1. Various additional generators.

5.1.2.2.1。各种额外的发电机

回答by CSchulz

HibernateDocsays 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。

回答by kervin

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

回答by Ahmad R. Nazemi

Unknown Id.generator: hibernate-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;
}

回答by Sumit

@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 已弃用。

回答by senjin.hajrulahovic

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 idcolumn with the type of BINARY(16)in MySQL or it's equivalent in other SQL implementations.

在 MySQL 中定义id类型为的列,BINARY(16)或者在其他 SQL 实现中等价。

回答by Sergey Ponomarev

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:

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

  • consumed size is more than BINARY(16)
  • after hydration the java.lang.Stringinstance consumes more memory than java.util.UUID: 112 bytes for UUID as string versus 32 bytes (i.e. two longs + obj header) for UUID.
  • 消耗的大小超过 BINARY(16)
  • 水化后,java.lang.String实例消耗的内存多于java.util.UUID:UUID 为字符串的 112 字节与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 上测试

回答by JavierFuentes

With current 5.4.2 Hibernate version,

使用当前的 5.4.2 Hibernate 版本,

if you want a Human-Readablevarchar(36)field in the database table,
but also a SerializableUUIDdata type in your Java Class,
you can use @Type(type = "uuid-char")at the same timeyou declare your field member with java.util.UUIDtype.

如果您想要数据库表中的Human-Readable varchar(36)字段,以及Java 类中
Serializable UUID数据类型,则
可以@Type(type = "uuid-char")使用java.util.UUIDtype声明字段成员的同时使用。

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;