1
  • 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.

2
  • What are you using to generate the schema? Are you using hibernate's DDL function or something like flyway? Commented Dec 8, 2020 at 16:53
  • I'm using a sql file under resources "schema.sql" CREATE SCHEMA IF NOT EXISTS cinc_student Commented Dec 8, 2020 at 19:10

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but this does not resolve the issue. I already had that in my application.properties. This is extremely intermittent. Works most of the time as it should.
1

Adding spring.jpa.generate-ddl=true to the application.properties worked for me.


  • Spring-boot version: 15.2
  • PostgreSQL version: v3.0.2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.