Spring JPA在数据库列上出现错误,返回与列名相关的错误。

huangapple go评论79阅读模式
英文:

Spring JPA error on DB column returns error related to column name

问题

我在尝试使用JPA查询Postgres数据库时收到了一个错误。

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
  datasource.schema=classpath:/schema.sql

引用此架构:

DROP TABLE student;

CREATE TABLE student

(

 id varchar(100) NOT NULL,

 name varchar(100) DEFAULT NULL,

 passportNumber varchar(100) DEFAULT NULL,

 PRIMARY KEY (id)

);

当我调用服务"/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();

}

我收到以下错误:

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_

如何实现消息中指定的提示 Perhaps you meant to reference the column "student0_.passportnumber"?我没有显式指定SQL,而是尝试仅使用JPA。我是否需要一个自定义查询,还是应该修改Student实体?

英文:

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(&quot;/students&quot;)
public ResponseEntity&lt;Object&gt; createStudent(@RequestBody Student student) {
    Student savedStudent = studentRepository.save(student);

    URI location = ServletUriComponentsBuilder.fromCurrentRequest().path(&quot;/{id}&quot;)
            .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 &quot;student0_.passportnumber&quot;.
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 &quot;student0_.passportnumber&quot; 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

得分: 4

JPA期望列名采用小写形式,每个单词之间用下划线分隔。这会转换为驼峰命名法的Java变量名。在您的情况下,您的Java变量名为passportNumber,对应于passport_number。有三种方法可以解决这个问题:

  1. 将Java变量重命名为passportnumber,这意味着它是一个单词。不幸的是,这看起来不太好,因为它违反了Java的变量命名约定。

  2. 将表列重命名为passport_number,以便正确连接JPA。

  3. 使用 @Column(name="passportNumber") 注解您的Java变量。

英文:

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").

huangapple
  • 本文由 发表于 2020年8月29日 05:55:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63641307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定