本文介绍了一个带有 PostgreSQL 的实体的多个 Hibernate 序列生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以为一个实体使用多个序列生成器吗,比如
Can I use Multiple sequence generators for one entity, like
@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;
推荐答案
不,你不能.生成器仅适用于标识符列.
No you can not. The generator are applicable for identifier columns only.
确保您使用脚本(例如 hibernate.hbm2ddl.import_files
)创建此序列:
Make sure you create this sequence with a script (e.g. 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;
这篇关于一个带有 PostgreSQL 的实体的多个 Hibernate 序列生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!