替换 @SequenceGenerator 因为它已被弃用

Replace @SequenceGenerator since its deprecated

我对 @SequenceGenerator 有疑问:

@SequenceGenerator(name="pk_user_id", sequenceName="seq_user_id", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="pk_user_id")

应用程序启动时显示警告:

WARN 7388 --- [ main] org.hibernate.orm.deprecation : HHH90000014: Found use of deprecated [org.hibernate.id.SequenceHiLoGenerator] sequence-based id generator; use org.hibernate.id.enhanced.SequenceStyleGenerator instead. See Hibernate Domain Model Mapping Guide for details

我试图找出如何用新代码替换已弃用的代码,但找不到任何解决方案。

根据警告消息和 Hibernate 文档(Hibernate deprecated list) you should use SequenceStyleGenerator. Or better use @GenericGenerator 并指定生成器策略。

这是一个典型的用法示例:

@GenericGenerator(
        name = "wikiSequenceGenerator",
        strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
        parameters = {
                @Parameter(name = "sequence_name", value = "WIKI_SEQUENCE"),
                @Parameter(name = "initial_value", value = "1000"),
                @Parameter(name = "increment_size", value = "1")
        }
)
@Id
@GeneratedValue(generator = "wikiSequenceGenerator")