gpt4 book ai didi

java - Hibernate 序列 ID 规范

转载 作者:行者123 更新时间:2023-11-30 02:28:56 25 4
gpt4 key购买 nike

我有这个注释来指定序列 ID:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "parametro_seq_gen")
@SequenceGenerator(name = "parametro_seq_gen", sequenceName = "PARAMETROS_SQ",
allocationSize = 1, initialValue = 1)

我发现在我的所有实体上重复非常冗长。

有什么方法可以创建自定义注释或其他东西吗?我只想指定序列名称

最佳答案

这很简单!

只需创建一个存储实体的package-info.java,并在其中提供全局@GenericGenerator:

@GenericGenerator(
name = "pooled",
strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator",
parameters = {
@Parameter(name = "sequence_name", value = "sequence"),
@Parameter(name = "initial_value", value = "1"),
@Parameter(name = "increment_size", value = "5"),
}
)
package com.vladmihalcea.book.hpjp.hibernate.identifier.globalsequence;

然后您的实体可以共享通用生成器,如下所示:

@Entity(name = "Post")
public class Post {

@Id
@GeneratedValue(generator = "pooled")
private Long id;
}

@Entity(name = "Announcement")
public class Announcement {

@Id
@GeneratedValue(generator = "pooled")
private Long id;
}

您需要使用@GenericGenerator,因为@SequenceGenerator不适用于包。

就是这样!

关于java - Hibernate 序列 ID 规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44741990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com