【发布时间】:2019-11-29 01:47:30
【问题描述】:
当我对表格做任何事情时,它总是显示错误:
Hibernate: select nextval ('hibernate_sequence')
2019-07-20 16:15:44.877 WARN 58376 --- [nio-9000-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 42P01
2019-07-20 16:15:44.877 ERROR 58376 --- [nio-9000-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: relation "hibernate_sequence" does not exist
我不想使用hibernate_sequence在表之间共享id序列,但想为每个表定义id seq并分别使用。
我使用Spring Boot 2.1.6.RELEASE、Spring Data JPA(Hibernate 5.3.10.Final)和Postgres 11.2,并定义了BigSerial类型的id字段,希望在各个实体中使用每个表的id序列类。
演示代码库在这里:https://github.com/Redogame/share_hibernate_sequence
创建用户表(使用身份作为表名,因为用户是 Postgres 保留关键字)。 通过定义bigserial类型的id,Postgres会自动创建一个identity_id_seq,我验证identity_id_seq已经创建成功了。
create table identity
(
id bigserial not null
constraint identity_pkey
primary key,
name varchar(255) not null
constraint identity_name_key
unique
constraint identity_name_check
check ((name)::text <> ''::text),
created_date timestamp not null,
created_by_id bigint not null
constraint identity_identity_id_fk
references identity,
last_modified_date timestamp not null,
last_modified_by_id bigint not null
constraint identity_identity_id_fk_2
references identity,
version bigint not null
);
指定一个序列生成器来使用这个id序列:
@Table(name = "identity")
public class UserEntity extends Auditable<Long> {
@Id
@SequenceGenerator(name="identity_id_seq", sequenceName = "identity_id_seq", initialValue=1, allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="identity_id_seq")
private Long id;
但它不起作用。我也试过配置spring.jpa.hibernate.use-new-id-generator-mappings和spring.jpa.properties.hibernate.id.new_generator_mappings,还是不行。
spring:
jpa:
hibernate:
use-new-id-generator-mappings: false
properties:
hibernate:
id:
new_generator_mappings: false
我希望不要使用hibernate_sequence,即:不要在任何SQL语句之前/之后执行select nextval('hibernate_sequence')。
【问题讨论】:
-
尝试手动创建一个序列,然后在映射文件中使用它。看它有效。然后我们就可以消除这个问题了。
-
@dassum 你想教我怎么做吗?请尝试在上面的 GitHub 存储库中更正我的源代码。
-
我已经发布了答案。如果解决了问题,请批准
标签: postgresql java-8 spring-data-jpa hibernate-5.x