繁体   English   中英

PostgreSQL Spring 引导 - hibernate_sequence 未生成

[英]PostgreSQL Spring Boot - hibernate_sequence is not generated

  • Java 11
  • Spring 启动 2.2.4.RELEASE
  • PostgreSQL 42.4.9

我一直在通过 Stack Overflow 和其他来源进行研究,但无法找到我的问题的答案。

基本上我有一个 Spring 引导应用程序,我正在尝试将 object 保存到 PostgreSQL 数据库。 尝试保存 object 时,我收到 hibernate_sequence 不存在错误。

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

这是我处理 id 生成的实体:

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

从我的 application.properties 文件中:

######### 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

我在资源下使用 schema.sql 文件,其中包含以下行来创建架构:

CREATE SCHEMA IF NOT EXISTS cinc_student

我有两个测试数据库,我将我的应用程序指向本地。 在其中一个数据库中,模式和 hibernate_sequence 是自动生成的,没有问题。

在另一个数据库中,创建了模式但未生成序列。 代码完全没有区别,只是指向不同的新创建的空本地数据库。

谁能解释一下?

我没有使用@GenerateSequence,因为我希望它使用应该开箱即用的默认 hibernate_sequence。 我不会创建手动序列。

将以下内容添加到application.properties 这将告诉 Spring 使用 Postgre 方言,它将在应用程序启动时自动创建 hibernate_sequence。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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