【问题标题】:Generate Uppercase Table names using Spring JPA for Postgres使用 Spring JPA for Postgres 生成大写表名
【发布时间】:2018-05-29 13:33:04
【问题描述】:

我正在尝试使用 Spring Jpa 从 postgres 获取记录。 Postgres 中的所有表和列都是大写的,但是 spring jpa 以小写生成查询,尽管我在实体类中提到了大写的表名。关于这个类似的问题,我已经阅读了很多关于 stackoverflow 的帖子,每个人都建议将命名策略添加到我所做的 yaml 文件中,但表名仍然以小写形式生成。我使用了以下策略(每次都使用但对我没有任何作用)。我还缺少其他东西吗?

spring.jpa.properties.hibernate.naming.physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.naming_strategy: org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.properties.hibernate.naming.implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
Caused by: **org.postgresql.util.PSQLException: ERROR: relation "**table_name**" does not exist**
  Position: 258
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2455)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2155)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:288)
        at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:430)
        at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:356)
        at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:168)
        at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:116)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114)
        at com.sun.proxy.$Proxy201.executeQuery(Unknown Source)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60)
        ... 179 common frames omitted

这是我添加到 yaml 文件中的属性:

spring.jpa.properties.database-platform: org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.id.new_generator_mappings: true
spring.jpa.properties.hibernate.cache.use_second_level_cache: true
spring.jpa.properties.hibernate.cache.use_query_cache: false
spring.jpa.properties.hibernate.generate_statistics: true
spring.jpa.properties.hibernate.cache.region.factory_class: io.github.jhipster.config.jcache.NoDefaultJCacheRegionFactory
spring.jpa.properties.hibernate.naming-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

        #hibernate.naming_strategy: org.hibernate.cfg.EJB3NamingStrategy
        #hibernate.naming.implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
        #hibernate.dialect: org.hibernate.dialect.PostgreSQLDialect

【问题讨论】:

  • 您尝试使用hibernate.globally_quoted_identifiers=true 还是简单地将表名放在引号中?
  • 另一个很好的例子,为什么你不应该使用带引号的标识符。您为什么首先创建具有大写名称的表?如果你从来没有引用过它们,你就不会遇到这个问题。
  • @a_horse_with_no_name:我这样做是故意的,因为我正在使用接收器连接器从 Kafka 填充/加载数据,并且 kafka 中的 Schema 注册表无法识别小写的表或列名,因为 schema-注册表默认以大写形式创建。
  • @Daria:不,我没有将“globally_quoted_identifiers”添加到我的属性中。添加后,表名是大写的,但也许 postgres 期望表名在引号内。我收到错误:原因:org.hibernate.MappingException:无法在 org.hibernate.mapping.Table(schema.TABLE_NAME) 及其相关的超级表和辅助表中找到具有逻辑名称的列:COLUMN_NAME ......在postgres DB中,当我运行将表名保留在引号内时它可以工作,如果没有引号则它不起作用。 Select * from schema."TABLENAME" (如果查询是这样工作的)
  • @AlfaRomeo 您是如何创建表格的?如果没有引号,则表名和列名强制小写

标签: spring postgresql hibernate jpa


【解决方案1】:

我已经用这种方式复制了你需要的东西:

CREATE TABLE temp."SOME_UPPERCASE_TABLE" (
  dt      TIMESTAMP NOT NULL DEFAULT now(),
  column1 VARCHAR(10) PRIMARY KEY
);

实体:

@Entity
@Table(name = "SOME_UPPERCASE_TABLE", schema = "tmp", catalog = "cram")
public class SomeUppercaseTableEntity {
    private Timestamp dt;
    private String column1;
    // getters, setters...

一切正常。

【讨论】:

    猜你喜欢
    • 2019-09-14
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2015-11-21
    • 2015-04-18
    • 1970-01-01
    • 2020-12-15
    • 2021-02-18
    相关资源
    最近更新 更多