UUIDGenerator 使用 new type() 而不是策略?

问题描述 投票:0回答:3

根据文档,不推荐使用以下用法:

@GenericGenerator(
            name = "UUID",
            strategy = "org.hibernate.id.UUIDGenerator"
    )

大家应该使用新的

type()
!但是我如何才能将上面的语句转换为对
type()
执行相同的操作呢?仅提供 hibernate UUIDGenerator 是行不通的。

java hibernate jpa uuid uuidgenerator
3个回答
13
投票

正如上面两条评论所暗示的那样,

type
现在属于类型类。因此修复很简单:

@GenericGenerator(
            name = "UUID",
            type = org.hibernate.id.uuid.UuidGenerator.class
    )

11
投票

使用新的注释@UuidGenerator

旧解决方案:

@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
private UUID id;

新解决方案(自 hibernate 6.2 起):

@Id
@UuidGenerator
private UUID id;

0
投票
@Id
@UuidGenerator(style = UuidGenerator.Style.TIME)
private UUID id;
© www.soinside.com 2019 - 2024. All rights reserved.