[英]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.
提前致谢。
you can read about ddl-auto-generation in spring doc您可以在 spring 文档中阅读有关 ddl-auto-generation 的信息
link: https://docs.spring.io/spring-boot/docs/1.0.x-SNAPSHOT/reference/html/howto-database-initialization.html链接: https : //docs.spring.io/spring-boot/docs/1.0.x-SNAPSHOT/reference/html/howto-database-initialization.html
Or或者
https://www.baeldung.com/spring-boot-data-sql-and-schema-sqlhttps://www.baeldung.com/spring-boot-data-sql-and-schema-sql
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.