我可以为一个实体使用多个序列生成器吗?
@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)创建此序列:
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;