在 Spring Boot 2 中,如何自动生成我的数据库并将模式生成命令记录到文件中?

sat*_*ish 5 jpa maven spring-data-jpa spring-boot spring-boot-2

我在 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
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

然而,当我在我的 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
Run Code Online (Sandbox Code Playgroud)

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

Pat*_*mil 1

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

应用程序属性

spring.jpa.properties.javax.persistence.schema-generation.database.action=update
Run Code Online (Sandbox Code Playgroud)

还建议使用或根据您正在使用的版本更改(已弃用) 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
Run Code Online (Sandbox Code Playgroud)