gpt4 book ai didi

java - 使用 PostgreSQL 的一个实体的多个 Hibernate 序列生成器

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:54:14 26 4
gpt4 key购买 nike

我可以为一个实体使用多个序列生成器吗,比如

@Id
@SequenceGenerator(name=”subscription_id_seq”,sequenceName=”subscription_id_seq”, allocationSize=7)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator=”subscription_id_seq”)
@Column(unique=true, nullable=false)
private Integer id

@Column(name="code", nullable=false, unique=true )
@SequenceGenerator(name="subscription_code_1_seq",sequenceName="subscription_code_1_seq", allocationSize=7)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="subscription_code_1_seq")
private Integer code;

最佳答案

不,你不能。生成器仅适用于标识符列。

确保使用脚本创建此序列(例如 hibernate.hbm2ddl.import_files):

create sequence subscription_code_1_seq start 1 increment 7

然后使用这样的映射:

@Id
@SequenceGenerator(
name="subscription_id_seq",
sequenceName="subscription_id_seq",
allocationSize=7
)
@GeneratedValue(
strategy=GenerationType.SEQUENCE,
generator="subscription_id_seq"
)
@Column(unique=true, nullable=false)
private Integer id;

@Column(
name="code",
nullable=false,
unique=true,
insertable = false,
updatable = false,
columnDefinition = "BIGINT DEFAULT nextval('subscription_code_1_seq')"
)
@Generated(GenerationTime.INSERT)
private Integer code;

关于java - 使用 PostgreSQL 的一个实体的多个 Hibernate 序列生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34528450/

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