Postgresql 使用postgres表序列而不是共享hibernate\u序列

Postgresql 使用postgres表序列而不是共享hibernate\u序列,postgresql,java-8,spring-data-jpa,hibernate-5.x,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-900

当我对表格执行任何操作时,它总是显示错误:

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_序列在表之间共享id序列,但想为每个表定义id seq并分别使用它们

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

演示回购协议如下:

创建用户表(使用标识作为表名,因为用户是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
);
我希望不使用hibernate\u序列,即:在任何SQL语句之前/之后不执行select nextval(“hibernate\u序列”)。

尝试以下步骤

  • 如果不存在,则创建序列手动\u seq

  • 更改创建表脚本

  • 创建表标识
    (
    id integer非空默认值nextval('manual_seq'::regclass),
    名称varchar(255)不为空
    约束标识\名称\密钥
    独特的
    约束标识\u名称\u检查
    选中((名称)::文本“”::文本),
    创建日期时间戳不为空,
    由_id创建的_bigint不为空,
    上次修改日期时间戳不为空,
    上次由\u id bigint修改的\u不为空,
    版本bigint不为空,
    约束手册\u seq\u pkey主键(id)
    );
    
    出于测试目的,我删除了外键约束

  • 更新实体映射
  • @实体
    @表(name=“identity”)
    @JsonIgnoreProperties(ignoreUnknown=true)
    公共类UserEntity扩展了Auditable{
    @身份证
    @SequenceGenerator(name=“manual seq”,sequenceName=“manual_seq”,allocationSize=1)
    @生成值(生成器=“手动顺序”)
    私人长id;
    @基本的
    @列(name=“name”,nullable=false)
    私有字符串名称;
    
    @MappedSuperclass
    @JsonIgnoreProperties({“new”、“createdDate”、“createdById”、“lastModifiedDate”、“lastModifiedById”、“version”})
    可审核的抽象类{
    @未经审核
    @创建数据
    @时态(TemporalType.TIMESTAMP)
    私人日期创建日期;
    @未经审核
    @创造的
    私有Long-createdById;
    @最后修改日期
    @时态(TemporalType.TIMESTAMP)
    私人日期最后修改日期;
    @最后修改
    私人长时间修改的bYID;
    @未经审核
    @版本
    私人长版;
    
    还原spring.jpa.hibernate.use-new-id-generator-mappings


    问题是扩展AbstractPersistable,因为没有使用哪个数据库序列。另外,请注意,出于测试目的,我已删除了审核。

    同样的问题也发生在我身上。我显式设置了
    spring.jpa.properties.hibernate.id.new\u generator\u mappings=false
    ,但
    选择nextval('hibernate_sequence')
    仍由hibernate运行

    我发现,当我们使用
    @GeneratedValue
    注释而不设置策略时,它默认为
    AUTO
    ,这意味着Hibernate将尝试使用
    Hibernate\u sequence
    生成ID值,然后它将失败,因为它在数据库中不存在

    因此,我创建了
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    并重试。在这种情况下,ID值是由数据库中的标识列(自动递增的主键)生成的,而不是由
    休眠\u序列生成的

    create table users (id serial not null, name varchar(250), primary key (id));
    

    请尝试手动创建一个序列,然后在映射文件中使用它。看看它是否有效。这样我们就可以消除这个问题。@dassum您想教我怎么做吗?请尝试在上面的GitHub repo中更正我的源代码。我已经发布了答案。如果它解决了问题,请批准。最后它仍然使用hibernate_序列。``Hibernate:选择userentity0.id作为id1_0_0,….Hibernate:选择nextval('identity_id_seq')Hibernate:插入到identity(由id创建、创建、上次修改、上次修改日期、版本、名称、id)值(?,,,,,,,?)Hibernate:选择nextval('Hibernate_sequence'))“``你用修改进行了测试吗?你能把你的差异粘贴在这里还是创建一个请求?@user2606091我用你的代码进行了测试。更改在抽象类Auditable中。不要扩展AbstractPersistable。
    spring:
      jpa:
        hibernate:
          use-new-id-generator-mappings: false
        properties:
          hibernate:
            id:
              new_generator_mappings: false
    
        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;
    
    create table users (id serial not null, name varchar(250), primary key (id));