1

I have been looking for hours for a solution to this problem, but I can't find it or solve it.

I'm using Hibernate to run several queries on tables that already exist, but I'm always getting the same error:

org.postgresql.util.PSQLException: ERROR: column config0_.pk does not exist
  Position: 8

My DB:

db

This is the Entity Class:

package org.package;
// default package
// Generated 16/12/2021, 14:24:34 by Hibernate Tools 4.3.5.Final

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;

/**
 * Config generated by hbm2java
 */

@Entity
@Table(name = "config", schema = "schema")
public class Config implements java.io.Serializable {

    private long pk;
    private Request request;
    private String key;
    private String value;
    private String description;
    private String createdBy;
    private Date createdDate;
    private String modifiedBy;
    private Date modifiedDate;
    private Set<ConfigInf> configInfs = new HashSet<ConfigInf>(0);

    public Config() {
    }

    public Config(long pk, String key, String value, String createdBy, Date createdDate) {
        this.pk = pk;
        this.key = key;
        this.value = value;
        this.createdBy = createdBy;
        this.createdDate = createdDate;
    }

    public Config(long pk, Request request, String key, String value, String description, String createdBy,
            Date createdDate, String modifiedBy, Date modifiedDate, Set<ConfigInf> configInfs) {
        this.pk = pk;
        this.request = request;
        this.key = key;
        this.value = value;
        this.description = description;
        this.createdBy = createdBy;
        this.createdDate = createdDate;
        this.modifiedBy = modifiedBy;
        this.modifiedDate = modifiedDate;
        this.configInfs = configInfs;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "PK", unique = true, nullable = false)
    public long getPk() {
        return this.pk;
    }

    public void setPk(long pk) {
        this.pk = pk;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "REQUEST_PK")
    public Request getRequest() {
        return this.request;
    }

    public void setRequest(Request request) {
        this.request = request;
    }

    @Column(name = "KEY", nullable = false, length = 256)
    public String getKey() {
        return this.key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    @Column(name = "VALUE", nullable = false, length = 1024)
    public String getValue() {
        return this.value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Column(name = "DESCRIPTION", length = 1024)
    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column(name = "CREATED_BY", nullable = false, length = 256)
    public String getCreatedBy() {
        return this.createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATED_DATE", nullable = false, length = 29)
    public Date getCreatedDate() {
        return this.createdDate;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    @Column(name = "MODIFIED_BY", length = 256)
    public String getModifiedBy() {
        return this.modifiedBy;
    }

    public void setModifiedBy(String modifiedBy) {
        this.modifiedBy = modifiedBy;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "MODIFIED_DATE", length = 29)
    public Date getModifiedDate() {
        return this.modifiedDate;
    }

    public void setModifiedDate(Date modifiedDate) {
        this.modifiedDate = modifiedDate;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "config")
    public Set<ConfigInf> getConfigInfs() {
        return this.configInfs;
    }

    public void setConfigInfs(Set<ConfigInf> configInfs) {
        this.configInfs = configInfs;
    }

}

My Repository:

I wanted to avoid using Native Queries, so use it like this, as represented. I also know that I can use the findAll or just " FROM Config", but nothing works and goes all to the same initial problem.

I would like to point out that Config is an Object, that is the Entity.

package org.package;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import pt.link.synchronizer.shared.domain.Config;

import java.util.List;

@Repository
public interface ConfigRepository extends JpaRepository<Config, Long> {

    @Query("SELECT c FROM Config c")
    List<Config> getAllConfigs();

}

The repository is then called through Autowired, and used in service, as shown below.

  //element -> convertConfigToDto(element)
            List<ConfigDTO> configs = configRepository.getAllConfigs().stream().map(element -> convertConfigToDto(element)).collect(Collectors.toList());

This is my application.properties:

Maybe I have too much in application.properties, but when trying to solve the problem, I saw that some important things were missing.

## Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.jpa.database=POSTGRESQL
spring.datasource.platform= postgres

spring.datasource.url=jdbc:postgresql://server:port/schema
spring.datasource.username=user
spring.datasource.password=pass
spring.datasource.driver-class-name=org.postgresql.Driver

# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.default_schema:schema

spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jmx.default-domain: umsspring.jpa.show-sql=true

spring.h2.console.enabled=true

When debugging, I can see and identify that the problem comes from the query call, in the repository.

I would like to point out that using Native Query = true, this problem disappears and everything works. (Ex: Using SELECT * FROM Config

Is it a problem in the database? Or is it some configuration I'm missing?

Thanks!

RESOLVED 07/01/2021

Solution: Change the columns name to Lower Case and removing the "".

1 Answers1

0

I would say "PK" and "pk" are two different things... At least from PostgreSQL's point of view. If you have upper-case at DB it should be called that way. If you have lowercase there - it can be called by lower/upper case. Except explicit call with " quotes. Put columns in your DB in lowercase, and it should work. Here you can see some solutions, you can play with - just in case that DB change is not possible.

Michał Zaborowski
  • 3,911
  • 2
  • 19
  • 39
  • 1
    the problem as that I was using Upper Case and "", and the postgres is anoying with does to combined. So the solution as to change all columns to Lower Case. – Marcelo Domingues Jan 07 '22 at 16:28