Hibernate注解映射sequence时出现无序增长问题+hibernate 映射 oracle ID自动增长:
通过Hibernate注解的方式映射oracel数据库的sequence主键生成器时,出现增加数据时sequence无序增长的问题,配置如下
@SequenceGenerator(name = "SEQ_DM_SERVICE_MODEL", sequenceName = SEQ_DM_SERVICE_MODEL")
@Id
@GeneratedValue(strategy = SEQUENCE, generator = "SEQ_DM_SERVICE_MODEL")
@Column(name = "SERVICE_MODEL_ID", unique = true, nullable = false, precision = 10, scale = 0)
public Long getId() {
return this.id;
}
此时主键id的增长是按照hibernate自动处理的方式,而并非数据库中定义的sequence来处理。必须加allocationSize=1,initialValue=1这两项配置才可以解决上述问题。如下:
@SequenceGenerator(name = "generator",allocationSize=1,initialValue=1, sequenceName = "SEQ_DM_SERVICE_MODEL")
====================================================================================================================================
安装有oracle数据库,创建数据库,总是要创建一个主键ID,唯一标示各条记录,但oracle不支持自动编号,所以还得创建一个SEQUENCE(序列)语句