【问题标题】:Spring Boot 2.2.2 and "hibernate.hbm2ddl.auto=(create|create-drop|update|validate)" not workingSpring Boot 2.2.2 和“hibernate.hbm2ddl.auto=(create|create-drop|update|validate)”不起作用
【发布时间】:2020-04-12 12:00:16
【问题描述】:

我的应用程序基于 Spring Boot 2.2.2.RELEASE 和 PostgreSQL。就持久性而言,我依赖于 Spring 的 AutoConfiguration。我的 application.properties 文件包含以下内容:

# Persistence
dbVendor=postgresql

# Basic connection options
spring.dataSource.driver-class-name=org.postgresql.Driver
spring.dataSource.url=jdbc:postgresql://is-0001/<database>
spring.dataSource.username=<username>
spring.dataSource.password=<password>

# Hibernate options
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=false
spring.jpa.properties.hibernate.hbm2ddl.auto=create
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.properties.hibernate.implicit_naming_strategy=<package>.ImplicitNamingStrategyImpl
spring.jpa.properties.hibernate.physical_naming_strategy=<package>.PhysicalNamingStrategyImpl

# Options to create sql scripts
spring.jpa.properties.javax.persistence.schema-generation.scripts.action=create
spring.jpa.properties.javax.persistence.schema-generation.scripts.create-target=/development/projects/<project>/backend/sql/setup/createDb.sql
#spring.jpa.properties.javax.persistence.schema-generation.scripts.drop-target=/development/projects/<project>/backend/sql/setup/dropDb.sql

由于某种原因,spring.jpa.properties.hibernate.hbm2ddl.auto=create 的设置被 Spring 忽略 - 与它的值无关 - 而 spring.jpa.properties.javax... 属性被正确应用,这很容易通过查看生成的 SQL 文件(createDb.sql 和 dropDb. sql)。

有人知道这种行为的原因可能是什么吗?我真的很感激,因为我已经尝试找到这个问题的根本原因超过一天了?

就像一个侧节点:Spring Boot 2.0.5.RELEASE 的行为是一样的。

【问题讨论】:

    标签: postgresql hibernate spring-boot jpa hbm2ddl


    【解决方案1】:

    你可以看看这里:

    https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-initialize-a-database-using-hibernate

    需要设置属性:spring.jpa.hibernate.ddl-auto=create

    【讨论】:

    • 感谢您的回答。当然,我阅读了您所指的文档。我的理解是spring.jpa.hibernate.ddl-auto=create 只是spring.jap.properties.hibernate.hbm2ddl.auto=create 的简写。而且我还测试了这个属性,结果完全相同——没有创建表。
    • 我可以确认我遇到了同样的问题。我尝试设置“spring.jpa.hibernate.ddl-auto”和“hibernate.hbm2ddl.auto”,但它们都没有被兑现。提供一些上下文 - 对我来说,这发生在我的单元测试环境中,因为这是我设置这些值的地方。
    猜你喜欢
    • 2018-07-06
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    • 1970-01-01
    • 2015-02-02
    • 2017-07-25
    相关资源
    最近更新 更多