我正在使用 postgres 和 Hibernate,并且我注意到生成的 id 有一些奇怪的地方。它在序列中产生了巨大的跳跃,我有一个有 1524 行的表,但最高 id 仍然是 602778。
我的 id 列定义如下:
id bigserial
并由 nextval('my_id_seq'::regclass) 支持
my_id_seq
的起始值为 1,增量为 1,当通过 SQuirreL 对其调用 nextval 时,增量会很好。
在我的 Hibernate 实体中,id 的映射如下:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
什么会导致 id 序列突然跳跃(在某个时刻它从 4152 跳跃到 12041)?
最佳答案
回滚和错误就可以做到这一点。 例如:
t=# create table s(i serial);
CREATE TABLE
t=# insert into s values (DEFAULT);
INSERT 0 1
t=# insert into s values (DEFAULT) returning i;;
i
---
2
(1 row)
INSERT 0 1
现在开始交易:
t=# begin;
BEGIN
t=# insert into s values (DEFAULT) returning i;
i
---
3
(1 row)
INSERT 0 1
t=# rollback;
ROLLBACK
使用了值 3,现在有一个间隙:
t=# insert into s values (DEFAULT) returning i;
i
---
4
(1 row)
INSERT 0 1
检查:
t=# select * from s;
i
---
1
2
4
(3 rows)
关于java - Hibernate 和 postgresql bigserial - 非顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41146960/