简体   繁体   中英

PostgreSQL Spring Boot - hibernate_sequence is not generated

  • Java 11
  • Spring Boot 2.2.4.RELEASE
  • PostgreSQL 42.4.9

I've been researching through Stack Overflow and other sources and have been unable to find an answer to my question.

Basically I have a Spring Boot application and I'm trying to save an object to a PostgreSQL db. When trying to save an object, I'm getting a hibernate_sequence does not exist error.

ERROR: relation "<schema>.hibernate_sequence" does not exist

Here is my entity that handles the id generation:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false, unique = true)
    private Long id;

From my application.properties file:

######### uses denoted schema (this schema must exist in db or have a .sql file in resources dir that creates it)
spring.jpa.properties.hibernate.default_schema=cinc_student
######### required to make spring use the schema.sql file in resources dir (if not using this file, this can be removed)
spring.datasource.initialization-mode=always
######### prevents hibernate from recreating unique indexes each time the app is restarted.
spring.jpa.properties.hibernate.schema_update.unique_constraint_strategy=RECREATE_QUIETLY

I'm using a schema.sql file under resources with the following line in it to create the schema:

CREATE SCHEMA IF NOT EXISTS cinc_student

I have two test databases that I point my application to locally. In one of the databases, the schema and hibernate_sequence were generated automatically without issue.

In the other db, the schema was created but the sequence was not generated. No difference in the code at all, just pointing to different freshly created empty local databases.

Can anyone explain?

I am not using a @GenerateSequence because I want it to use the default hibernate_sequence that should be generated out of the box. I will not be creating a manual sequence.

Add the following to application.properties . This will tell Spring to use the Postgre Dialect which will auto-create the hibernate_sequence on application startup.

spring.jpa.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM