用于PostgreSQL的一个实体的多个Hibernate序列生成器 [英] Multiple Hibernate sequence generators for one entity with PostgreSQL

查看:118
本文介绍了用于PostgreSQL的一个实体的多个Hibernate序列生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  @Id 
@SequenceGenerator(name =
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =subscription_id_seq)
@Column(unique = true,nullable = false)
私人整数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 $ b $ pre $ 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,
可更新= false,
columnDefinition =BIGINT DEFAULT nextval('subscription_code_1_seq')

@Generated(GenerationTime.INSERT)
private Integer code;


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.

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

Then use a mapping like this:

@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序列生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆