简体   繁体   English

当为 Postgres DB 的属性 spring.jpa.hibernate.ddl-auto 提供更新值时,Hibernate 不会生成序列

[英]Hibernate does not generate sequences when update value is provided to the property spring.jpa.hibernate.ddl-auto for Postgres DBs

When I use spring.jpa.hibernate.ddl-auto=create, sequences and tables are both created but when this property is set to update, only the tables are getting created.当我使用 spring.jpa.hibernate.ddl-auto=create 时,序列和表都被创建,但是当这个属性设置为更新时,只有表被创建。 I have created a spring boot application and for DB, I am using Postgres.我已经创建了一个 Spring Boot 应用程序,对于 DB,我使用的是 Postgres。 But when I check the same for h2 database (which is embedded), the sequences and the tables get created for the update value as well.但是,当我对 h2 数据库(嵌入的)进行相同检查时,也会为更新值创建序列和表。 I can not set the property to create as it will drop the tables when the application restarts.我无法设置要创建的属性,因为它会在应用程序重新启动时删除表。 Can somebody please enlighten me?有人可以启发我吗? Thanks in advance.提前致谢。

There was a bug in hibernate related to Postgres dialect.休眠中存在与 Postgres 方言相关的错误。 Below is the method in PostgresSQLDialect to check if sequence is present in schema (which is wrong by the way):以下是 PostgresSQLDialect 中检查模式中是否存在序列的方法(顺便说一下,这是错误的):

@Override
    public String getQuerySequencesString() {
        return "select relname from pg_class where relkind='S'";
    }

The above query returns sequences if there is any sequence present in any schema in the database.如果数据库中的任何模式中存在任何序列,则上述查询将返回序列。 And if there is any schema containing any sequence, it won't create sequences in the specified schema of that Database.如果有任何包含任何序列的模式,它不会在该数据库的指定模式中创建序列。 To resolve this, I wrote a custom Postgres dialect and changed the query to following:为了解决这个问题,我编写了一个自定义 Postgres 方言并将查询更改为以下内容:

@Override
public String getQuerySequencesString() {
    return "select sequencename from pg_catalog.pg_sequences where schemaname='"+schemaName+"'";
}

It resolves the issue when ddl-auto is set to update.当 ddl-auto 设置为更新时,它解决了这个问题。 The sequences are getting created now.现在正在创建序列。

暂无
暂无

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

相关问题 “spring.jpa.hibernate.ddl-auto”属性用于迁移? - "spring.jpa.hibernate.ddl-auto" property is used for migration? spring.jpa.hibernate.ddl-auto=更新在 Z2A2D5925E6ED9AB134D636 中不起作用 - spring.jpa.hibernate.ddl-auto=update not working in spring boot spring.jpa.hibernate.ddl-auto = validate属性是否验证带有表的列? - Does spring.jpa.hibernate.ddl-auto = validate property validates the columns with table? Hibernate Spring: spring.jpa.hibernate.ddl-auto property doesn't create generate tables doesn't work - Hibernate Spring: spring.jpa.hibernate.ddl-auto property doesn't create generate tables doesn't work 在springboot启动期间验证“ spring.jpa.hibernate.ddl-auto” - Validate “spring.jpa.hibernate.ddl-auto” during springboot startup spring.jpa.hibernate.ddl-auto = update mariadb无法自动创建数据库索引或外键或列 - spring.jpa.hibernate.ddl-auto=update mariadb can not auto create Database indexes or Foreign key or column 似乎 JPA spring.jpa.ZCB1F008EEBF5012C4EF419A2Cldd-autoZ77 不起作用。 - Seems JPA spring.jpa.hibernate.ddl-auto=update doesn't work "Hibernate-Spring boot : spring.jpa.hibernate.ddl-auto=update 更新实体后重新创建表和列" - Hibernate-Spring boot : spring.jpa.hibernate.ddl-auto=update recreates tables and columns after update the entity application.properties 中 spring.jpa.hibernate.ddl-auto=update 的问题 - Problem with spring.jpa.hibernate.ddl-auto=update in application.properties Spring spring.jpa.hibernate.ddl-auto等于validate时Boot 2.3启动失败 - Spring Boot 2.3 fails to start when spring.jpa.hibernate.ddl-auto equals to validate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM