【问题标题】:Table not found - wrong naming strategy?未找到表 - 命名策略错误?
【发布时间】:2017-12-17 15:12:39
【问题描述】:

我可以通过 pgAdmin 清楚地看到公共方案中存在表“TEST”。

这是我的配置:

@Entity
@Table(name = "TEST", schema = "public")
public class TestEntity

application.yml:

spring:
  jpa:
    hibernate:
      ddl-auto: validate
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
    databasePlatform: org.hibernate.dialect.PostgreSQLDialect

例外:

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [TEST]
        at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateTable(SchemaValidatorImpl.java:67)

当我将 ddl-auto 更改为“更新”时,它会创建带有“测试”名称的表,并且一切都开始按预期工作。问题是我认为我的配置很好,它应该找到“TEST”。 有人可以帮忙吗?也许我需要使用更具体的方言?

【问题讨论】:

  • 根据我的经验,如果您的架构没问题,此类错误与应用程序 yaml 中的数据源定义有关。

标签: spring hibernate jpa spring-boot


【解决方案1】:

原因当然是命名策略。

  1. 当你显式指定表名时,像这里@Table(name = "TEST") 只有PhysicalNamingStrategy 可以更改它(相反ImplicitNamingStrategy)。
  2. SpringPhysicalNamingStrategy 将名称更改为小写,从源代码中可以看到:SpringPhysicalNamingStrategy.java。来自那里的代码 sn-p:

    protected Identifier getIdentifier(String name, boolean quoted, JdbcEnvironment jdbcEnvironment) { if (isCaseInsensitive(jdbcEnvironment)) { name = name.toLowerCase(Locale.ROOT); } return new Identifier(name, quoted); }

  3. org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 什么都不做。所以表名应该是你指定的TEST。但是,看起来,您在 spring-boot 配置中错误地指定了它。您应该仔细检查属性名称。

  4. 您还可以检查其他属性文件(可能是您有其他 jar)以获取该属性值。

另外,为了测试,你可以尝试使用application.properties

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

【讨论】:

    【解决方案2】:

    不知道你的hibernate 版本我猜这可能是hibernate 功能,例如here。

    在这种情况下,解决方案是将表名更改为小写

    @Table(name = "TEST", schema = "public")
    

    到

    @Table(name = "test", schema = "public")
    

    按照here的建议。

    根据您的数据库,也可以通过用转义的双引号将表包装成大写来创建表。

    @Table(name = "\"TEST\"", schema = "public")
    

    后者至少适用于 Postgres。

    【讨论】:

      猜你喜欢
      • 2016-04-21
      • 1970-01-01
      • 2017-01-02
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      相关资源
      最近更新 更多