theRoot
theRoot

Reputation: 569

spring boot + hibernate table names queried in lower case

I'm new to Spring boot and hibernate.

Below is my application prop file and the naming strategy implementation file. When I try to push data to DB the below exception occurs ERROR: relation "nickname" does not exist.

I want access table name and column name always in uppercase.

Please help me to understand what went wrong.enter image description here

NickName.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());
    }

}

Exception:

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]

Update 1: changed app.prop file as below

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

Note :

In my sql client just now checked

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

Upvotes: 0

Views: 3124

Answers (1)

Mani
Mani

Reputation: 111

This should work :

1) Get rid of PhysicalNamingStrategyImpl.java

2) Make changes in application.properties as below :

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) In @Table add schema as well. ie., @Table(name="table", schema="schemaname")

This worked for me with Postgresql 10 with Hibernate 5.x and Spring Boot 2.1.x. If you are using an older version of Postgresql, then use appropriate PostgreSqlxxDialect from Spring.

Upvotes: 1

Related Questions