【问题标题】:spring boot + hibernate table names queried in lower casespring boot + hibernate 小写查询表名
【发布时间】:2018-10-02 01:09:43
【问题描述】:

我是 Spring Boot 和休眠的新手。

下面是我的应用程序道具文件和命名策略实现文件。 当我尝试将数据推送到数据库时,出现以下异常错误:关系“昵称”不存在。

我希望访问表名和列名总是大写。

请帮助我了解问题所在。

昵称.java

@Entity
@Table(name="NICKNAME")
public class NickName {

application.properties

spring.datasource.url=jdbc:postgresql://192.168.239.129:5432/maindb
spring.datasource.username=pgdbuser
spring.datasource.password=pgdbuser
spring.jpa.generate-ddl=false
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=DEBUG
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL82Dialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

PhysicalNamingStrategyImpl.java

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl;
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;

public class PhysicalNamingStrategyImpl extends PhysicalNamingStrategyStandardImpl implements Serializable{

    public static final PhysicalNamingStrategyImpl INSTANCE = new PhysicalNamingStrategyImpl();

    @Override
    public Identifier toPhysicalTableName(Identifier name, JdbcEnvironment context) {
         String nameModified = StringUtils.upperCase(name.getText());

        // Do whatever you want with the name modification
        return new Identifier(nameModified, name.isQuoted());
    }


    @Override
    public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment context) {
        String nameModified = StringUtils.upperCase(name.getText());

        // Do whatever you want with the name modification
        return new Identifier(nameModified, name.isQuoted());
    }

}

例外:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:242) ~[spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:225) ~[spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527) ~[spring-orm-5.0.5.RELEASE.jar:5.0.5.RELEASE]
......
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
    at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) ~[hibernate-core-5.2.16.Final.jar:5.2.16.Final]
.......
Caused by: org.postgresql.util.PSQLException: ERROR: relation "nickname" does not exist
  Position: 460
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433) ~[postgresql-42.2.2.jar:42.2.2]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178) ~[postgresql-42.2.2.jar:42.2.2]

更新 1:更改 app.prop 文件如下

spring.datasource.url=jdbc:postgresql://192.168.239.129:5432/maindb
spring.datasource.username=pgdbuser
spring.datasource.password=pgdbuser
spring.jpa.generate-ddl=false
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate5.SpringSessionContext
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=DEBUG
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.hibernate.naming.physical-strategy=com.theroot.rester.PhysicalNamingStrategyImpl

注意:

在我刚才检查的sql客户端中

select * from "NICKNAME"; --Works
select * from NICKNAME; --doesn't Work

【问题讨论】:

  • spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl PhysicalNamingStrategyImpl
  • @krokodilko 更新问题。谢谢
  • 因此您必须使用双引号创建表 - CREATE TABLE "TABLE_NAME" (....),请参阅此演示以了解 postgresql 创建表命令的工作原理:sqlfiddle.com/#!17/81c0a/1
  • 是的,我认为导致问题,将检查并返回.. 将删除双引号并尝试
  • 是的,删除列名中的双引号(在 sql DDL 中)解决了这个问题

标签: postgresql hibernate spring-boot


【解决方案1】:

这应该可行:

1) 摆脱PhysicalNamingStrategyImpl.java

2) 在 application.properties 中进行如下更改:

spring.jpa.properties.hibernate.dialect = 
org.hibernate.dialect.PostgreSQL95Dialect spring.jpa.hibernate.naming.implicit- 
strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl 
spring.jpa.hibernate.naming.physical-strategy= 
org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 
spring.jpa.properties.hibernate.globally_quoted_identifiers=true

3) 在@Table 中也添加架构。即@Table(name="table", schema="schemaname")

这对我使用了带有 Hibernate 5.x 和 Spring Boot 2.1.x 的 Postgresql 10。如果您使用的是旧版本的 Postgresql,请使用 Spring 中适当的 PostgreSqlxxDialect。

【讨论】:

    猜你喜欢
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 1970-01-01
    • 2017-07-15
    • 2018-05-15
    • 2018-03-17
    相关资源
    最近更新 更多