Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Hibernate sequence generators for one entity with PostgreSQL

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;
like image 842
Deepak Avatar asked Dec 25 '22 11:12

Deepak


1 Answers

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;
like image 97
Vlad Mihalcea Avatar answered Dec 30 '22 11:12

Vlad Mihalcea