2

I receive an error when trying to query a Postgres DB using JPA.

Student.java:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Student {
    @Id
    @GeneratedValue
    private Long id;
    private String name;
    private String passportNumber;

    public Student() {
        super();
    }

    public Student(Long id, String name, String passportNumber) {
        super();
        this.id = id;
        this.name = name;
        this.passportNumber = passportNumber;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassportNumber() {
        return passportNumber;
    }
    public void setPassportNumber(String passportNumber) {
        this.passportNumber = passportNumber;
    }

}

appliction.properties:

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect

spring.jpa.hibernate.ddl-auto=none

spring.jpa.hibernate.show-sql=true

spring.datasource.url=jdbc:postgresql://localhost:5432/shorten-db

spring.datasource.username=my_user

spring.datasource.password=my_password

spring.datasource.initialization-mode=always

spring.datasource.initialize=true

spring.datasource.schema=classpath:/schema.sql

spring.datasource.continue-on-error=true

appliction.yml:

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/shorten-db
    username: my_user
    password: my_password
    driverClassName: org.postgresql.Driver

 spring.datasource.schema=classpath:/schema.sql

refers to this schema:

DROP TABLE student;

CREATE TABLE student

(

 id varchar(100) NOT NULL,

 name varchar(100) DEFAULT NULL,

 passportNumber varchar(100) DEFAULT NULL,

 PRIMARY KEY (id)

);

When I invoke the service "/students" :

@PostMapping("/students")
public ResponseEntity<Object> createStudent(@RequestBody Student student) {
    Student savedStudent = studentRepository.save(student);

    URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
            .buildAndExpand(savedStudent.getId()).toUri();

    return ResponseEntity.created(location).build();

}

I receive the following error:

postgres-db    | 2020-08-28 21:46:28.108 UTC [257] HINT:  Perhaps you meant to reference the column "student0_.passportnumber".
postgres-db    | 2020-08-28 21:46:28.108 UTC [257] STATEMENT:  select student0_.id as id1_1_, student0_.name as name2_1_, student0_.passport_number as passport3_1_ from student student0_

How to implement the hint specified in the message Perhaps you meant to reference the column "student0_.passportnumber" I'm not explicitly specifying the SQL and trying to use JPA only. Do I need a custom query or should I modify the student entity ?

1 Answer 1

4

JPA expects column names in the form of lower case and each word separated by an underscore. This translates to a camel cased java variable names. In your case, you have java variable name as passportNumber which translates to passport_number. There are three ways this can be solved:

  1. Rename java variable to passportnumber, meaning its a single word. Unfortunately, it doesn't look so good since it breaks the variable naming convention of java.

  2. Rename table column to passport_number, hence making correct JPA connection.

  3. Annotate your java variable with @Column(name="passportNumber").

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.