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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With