问题描述
我有一个使用Hibernate映射到postgres数据库的模型类.我的模型课是:
I have a model class that is mapped to a postgres database using hibernate. My model class is:
CheckRes.java
package com.example.demo.model;
import javax.persistence.Id;
import javax.persistence.*;
@Entity
@Table(name="checkers",schema = "public")
public class CheckRes {
public long getID() {
return ID;
}
public void setID(long ID) {
this.ID = ID;
}
public String getCheck() {
return check;
}
public CheckRes() {
}
public void setCheck(String check) {
this.check = check;
}
@Id
private long ID;
public CheckRes(String check) {
this.check = check;
}
@Column(name="check")
private String check;
}
application.properties
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.EJB3NamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.hibernate.ddl-auto = update
下表已存在于db中,其创建脚本为:
The following table is already existing in db with create script as:
CREATE TABLE public."checkers"
(
"ID" bigint NOT NULL,
"check" character varying(20),
CONSTRAINT "checkers_pkey" PRIMARY KEY ("ID")
)
TABLESPACE pg_default;
ALTER TABLE public."checkers"
OWNER to postgres;
稍后,当我尝试从邮递员调用controller get方法时,出现以下错误.
Later when I am trying to invoke the controller get method from a postman , I am getting the following error.
如果该表不存在,那么休眠将自动创建一个带有'_'的表,并且没有错误.但是我不需要休眠来创建任何表.它只需要使用现有的CRUD操作,我还有其他命名约定吗?
If the table doesnt exist then hibernate automatically creates a table with '_' and there are no errors. But I dont need the hibernate to create any tables. It just needs to use the existing ones for CRUD operations,is there any other naming convention I am missing?
推荐答案
通常,遵循 java命名约定.据此,您应该将 ID
字段重命名为 id
.
Generally, this is a good practice to follow java naming conventions. According to that you should rename ID
field to id
.
然后@ User-Upvotedon'tsayThanks和@a_horse_with_no_name注意到,您应该将 @Column
与引号:
And then as was noticed by @User-Upvotedon'tsayThanks and @a_horse_with_no_name you should use @Column
with quotes:
@Id
@Column(name="`ID`")
private long id;
您的获取器和设置器应该遵循 JavaBean约定.因此,对于上述字段,您应该具有以下getter和setter:
Also your getters and setters should follow JavaBean conventions. So, for the above field you should have the following getter and setter:
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
这篇关于休眠错误column0_.id不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!