下面是一个简单的例子: 使用的数据库是 PostgresSQL。使用的堆栈:Spring Boot + Hibernate + FlyWay
@数据
@Entity
@Table(name = "books")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long bookId;
//....
}
这是hibernate生成的表
create table if not exists books
(
book_id bigserial not null
constraint books_pkey
primary key,
language_id bigint
constraint fksp7ty25kndaxyfrgkrloo1dd8
references author
);
用于导入的 sql 查询示例:
INSERT INTO "public"."books" ("book_id", "author_id") VALUES (1, 2);
通过flyway序列的数据迁移后未更新。那些。我应该有超过 200 个对象,并且序列索引显示 1。
SELECT last_value FROM books_book_id_seq;
last_value 0
在这种情况下,例如,对于由 java 脚本初始化的另一个实体,索引会正确显示。问题与此类似: 链接
如果 GenerationType.IDENTITY ,为什么休眠会生成序列?我的错误是什么?
更新
尝试了其他方法 - 更改了 GenerationType.SEQUENCE hibernate created hibernate_sequence 但也将表 DDL 更改为(bigserial -> bigint):
create table if not exists books
(
book_id bigint not null
constraint books_pkey
primary key,
language_id bigint
constraint fksp7ty25kndaxyfrgkrloo1dd8
references author
);
在任何情况下,当通过 flyway 迁移时,该值都不会增加。我可以手动更正此值的最大值。
好吧,既然序列中的值不正确,那么这里就是异常——DataIntegrityViolationException
提前感谢您的回复!