当我对表执行任何操作时,它总是显示错误:
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')。
答案 0 :(得分:0)
尝试以下步骤
如果不存在则创建序列manual_seq;
更改创建表脚本
create table identity
(
id integer NOT NULL DEFAULT nextval('manual_seq'::regclass),
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,
last_modified_date timestamp not null,
last_modified_by_id bigint not null,
version bigint not null,
CONSTRAINT manual_seq_pkey PRIMARY KEY (id)
);
出于测试目的,我删除了外键约束。
@Entity
@Table(name = "identity")
@JsonIgnoreProperties(ignoreUnknown = true)
public class UserEntity extends Auditable<Long> {
@Id
@SequenceGenerator(name="manual-seq", sequenceName = "manual_seq",allocationSize = 1)
@GeneratedValue(generator="manual-seq")
private Long id;
@Basic
@Column(name = "name", nullable = false)
private String name;
@MappedSuperclass
@JsonIgnoreProperties({"new", "createdDate", "createdById", "lastModifiedDate", "lastModifiedById", "version"})
abstract class Auditable<PK extends Serializable>{
@NotAudited
@CreatedDate
@Temporal(TemporalType.TIMESTAMP)
private Date createdDate;
@NotAudited
@CreatedBy
private Long createdById;
@LastModifiedDate
@Temporal(TemporalType.TIMESTAMP)
private Date lastModifiedDate;
@LastModifiedBy
private Long lastModifiedById;
@NotAudited
@Version
private Long version;
还原spring.jpa.hibernate.use-new-id-generator-mappings
该问题正在扩展 AbstractPersistable ,因为未使用哪个数据库序列。另外,请注意,出于测试目的,我已删除了审核。
答案 1 :(得分:0)
我也遇到了同样的问题。我明确设置了spring.jpa.properties.hibernate.id.new_generator_mappings=false
,但是select nextval ('hibernate_sequence')
仍然由Hibernate运行。
我发现,当我们使用@GeneratedValue
批注而不设置策略时,它的默认值为AUTO
,这意味着Hibernate将尝试使用hibernate_sequence
生成ID值,然后会失败,因为它在数据库中不存在。
因此,我做了@GeneratedValue (strategy = GenerationType.IDENTITY)
,然后重试。在这种情况下,该ID值是由我在数据库中的标识列(自动递增的主键)而不是由hibernate_sequence
生成的。
create table users (id serial not null, name varchar(250), primary key (id));