5

我正在使用带有 Java 11 的 Spring Boot 2.1。我正在使用 Maven 来构建我的工件。在本地运行时,我喜欢让我自​​动创建数据库的 Spring JPA 指令......

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true

我也喜欢让我自​​动创建文件的指令......

spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql

但是,当我将两者结合在我的 src/main/resources/application.properties ...

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.jpa.properties.javax.persistence.validation.mode=none

spring.datasource.url=jdbc:postgresql://${PG_DB_HOST:localhost}:5432/${PG_DB_NAME}

spring.datasource.username=${PG_DB_USER}
spring.datasource.password=${PG_DB_PASS}

spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql

似乎“spring.jpa.properties.javax.persistence”优先,我的架构更改不会针对数据库自动运行。有没有办法配置两者都发生 - 更改被记录到文件并自动针对我的数据库运行?

4

1 回答 1

1

如下添加带有javax.persistence的database.action ,这将根据数据库模式创建中所述的模型更新数据库模式

应用程序属性

spring.jpa.properties.javax.persistence.schema-generation.database.action=update

还建议使用或根据您使用的版本更改(已弃用) PostgreSQLDialect方言。方言PostgreSQL82Dialect

应用程序属性

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.jpa.properties.javax.persistence.validation.mode=none

spring.datasource.url=jdbc:postgresql://${PG_DB_HOST:localhost}:5432/${PG_DB_NAME}

spring.datasource.username=${PG_DB_USER}
spring.datasource.password=${PG_DB_PASS}

spring.jpa.properties.javax.persistence.schema-generation.database.action=update
spring.jpa.properties.javax.persistence.schema-generation.create-source=metadata
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=update
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=update.sql
于 2020-07-12T05:12:51.637 回答