Hibernate+Postgresql: PK not starting from 1 (PG sequence created from 1 starting)
Hibernate+Postgresql: PK not starting from 1 (PG sequence created starting from 1)
我有一个 table,主键是 SERIAL 类型。
此主键受如下序列影响:
CREATE SEQUENCE swq
INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807
START 1 CACHE 1;
我的问题是:为什么主键在第一次执行 saveorUpdate()
方法后创建了第一条记录,但将 id 字段插入为零 (0) 而不是一个 (1)?
来自 OP 的评论:
@Entity @Table(name = "table", schema = "schem")
public class tableBE implements java.io.Serializable
{
private int idmotiv;
@Id @Column(name = "_id", unique = true, nullable = false)
public int getIdmot() { return this.idmot; }
}
使用@GeneratedValue 和@SequenceGenerator 注解配置。下面是例子。
@Id
@GeneratedValue(generator="Hib_Seq")
@SequenceGenerator(name="Hib_Seq", sequenceName="swq")
private int idmotiv;
我有一个 table,主键是 SERIAL 类型。 此主键受如下序列影响:
CREATE SEQUENCE swq
INCREMENT 1 MINVALUE 1 MAXVALUE 9223372036854775807
START 1 CACHE 1;
我的问题是:为什么主键在第一次执行 saveorUpdate()
方法后创建了第一条记录,但将 id 字段插入为零 (0) 而不是一个 (1)?
来自 OP 的评论:
@Entity @Table(name = "table", schema = "schem")
public class tableBE implements java.io.Serializable
{
private int idmotiv;
@Id @Column(name = "_id", unique = true, nullable = false)
public int getIdmot() { return this.idmot; }
}
使用@GeneratedValue 和@SequenceGenerator 注解配置。下面是例子。
@Id
@GeneratedValue(generator="Hib_Seq")
@SequenceGenerator(name="Hib_Seq", sequenceName="swq")
private int idmotiv;