postgresql - 使用 postgres 表序列而不是共享 hibernate_sequence

标签 postgresql java-8 spring-data-jpa hibernate-5.x

当我对表格做任何事情时,它总是显示错误:

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 sequence,而是想为每个表定义 id seq 并分别使用它们。

我使用Spring Boot 2.1.6.RELEASE、Spring Data JPA(Hibernate 5.3.10.Final)、Postgres 11.2,定义id字段为BigSerial类型,希望在各自实体中使用各表的id序列类。

演示仓库在这里:https://github.com/Redogame/share_hibernate_sequence

创建用户表(使用 identity 作为表名,因为 user 是 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-mappingsspring.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')。

最佳答案

尝试以下步骤

  1. 如果不存在则创建序列 manual_seq;

  2. 修改建表脚本

    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)
    );

出于测试目的,我删除了外键约束。

  1. 更新实体映射
    @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 因为没有使用数据库序列。另请注意,出于测试目的,我已删除审核。

关于postgresql - 使用 postgres 表序列而不是共享 hibernate_sequence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57123067/

相关文章:

java - 如何对Map of Map使用双括号初始化

postgresql - 从远程机器连接到 RDS Postgres

sql - Postgres : regex and nested queries something like Unix pipes

java - 使用java流获取逗号分隔的字符串

java - 当我使用继承实体的存储库时如何修复 "No property [repositoryMethod] found for type [entity]!"?

java - Spring-Data-Jpa 在持久化后清除链接实体的所有参数

java - 查找具有多个角色的用户作为 jpa 标准

sql - 使用哪种语言编写 PostgreSQL 脚本?

postgresql - 是否可以使用 postgres/psql COPY 到表的特定分区?

java - 在 map 中加入 List<String>