Search code examples
javapostgresqlspring-boothibernate

Incorrect column name when querying PostgreSQL database from Spring Boot app using Hibernate


I have a simple Spring Boot application that connects to a PostgreSQL database.

This is a piece of Photographer class (entity)

@Entity
@Table(name = "photographer", schema = "public")
@Data
@NoArgsConstructor
public class Photographer {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String additionalInfo;
    private String location;
    private String portfolio;
...
}

This is how the table is created in Liquibase:

<createTable tableName="photographer">
    <column name="id" type="bigint">
        <constraints primaryKey="true" primaryKeyName="photographer_id_pk" />
    </column>
    <column name="name" type="varchar(100)"/>
    <column name="additionalInfo" type="varchar(100)"/>
    <column name="location" type="varchar(100)"/>
    <column name="portfolio" type="varchar(100)"/>
    <column name="linksToSocials" type="varchar(100)"/>
    <column name="offer" type="varchar(255)"/>
    <column name="reviews" type="varchar(100)"/>
    <column name="commuting" type="varchar(100)"/>
    <column name="devices" type="varchar(100)"/>
</createTable>

This is the JpaRepository interface:

@Repository
public interface PhotographerRepository extends JpaRepository<Photographer, Long> {
}

When I try to invoke findAll() method I get following error:

org.postgresql.util.PSQLException: ERROR: column p1_0.additionalinfo does not exist

I checked and the actual name of the column in database is additionalInfo with uppercased "I", but Hibernate is trying to use the lowercase column name "additionalinfo"

I tried setting different dialects and strategies using these configs:

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

What else can I try? I know that the workaround may be changing column names to snake case, or adding @Column annotation, but I am looking for a way to force Hibernate to construct correct query with correct column name (the same as class property name)

Edit after comment:

Solution from How to configure Hibernate to make table/column name with uppercase AND lowercase letters does not solve my issue. In the mentioned case, the problem was convert snake case to camel case (example_name to exampleName). In my case I have property in a class with name additionalInfo. The same name is in the column. When Hibernate constructs query it uses name additionalinfo. So my case is converting from examplename to exampleName

Answer from comments solves the issue, which is:

spring.jpa.properties.hibernate.globally_quoted_identifiers=true

Solution

  • Answer from comments solves the issue, which is:

    spring.jpa.properties.hibernate.globally_quoted_identifiers=true