【问题标题】:In Spring Boot 2, how can I auto-generate my database and also record the schema generation commands to a file?在 Spring Boot 2 中,如何自动生成数据库并将模式生成命令记录到文件中?
【发布时间】:2020-10-29 12:18:47
【问题描述】:

我正在使用带有 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”优先,并且我的架构更改不会针对数据库自动运行。有没有办法配置这两种情况——更改被记录到文件中并自动针对我的数据库运行?

【问题讨论】:

    标签: spring-boot maven jpa spring-data-jpa spring-boot-2


    【解决方案1】:

    添加 database.actionjavax.persistence 如下,这将根据 Database Schema Creation 中解释的模型更新数据库模式

    application.properties

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

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

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

    【讨论】:

    • 我正在使用 Spring Boot,并且我正在阅读 Spring Boot 忽略了 persistence.xml -- medium.com/@nieldw/… 。有没有办法将该属性包含在 application.properties 文件中?
    • 是的,我已经为使用 xml 配置的人添加了。由于您的项目包含应用程序属性文件,因此您只需按照答案中的描述包含spring.jpa.properties.javax.persistence.schema-generation.database.action=update
    猜你喜欢
    • 2012-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 2020-11-29
    相关资源
    最近更新 更多