Hibernate:PSQLException:错误:列“this_.user_id”不存在

时间:2017-07-18 13:36:11

标签: java postgresql hibernate

我知道这个问题已经被多次问到stackoverflow,我已经完成了所有这些问题。但没有什么对我有用。所以我再次发布这个问题。我在postgres有一张桌子。 DDL在下面。

CREATE TABLE  APPUSERMASTER (
 USER_ID BIGSERIAL NOT NULL,
 USER_NAME VARCHAR(20) NOT NULL,
 FIRST_NAME VARCHAR(20) NOT NULL,
 SECOND_NAME VARCHAR(20) NOT NULL,
 EMAIL VARCHAR(50) NOT NULL,
 PASSWORD TEXT  NOT NULL,
 PRIMARY KEY (USER_ID) 
) ;

我的POJO课程如下。

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

private long userId;
private String userName;
private String firstName;
private String secondName;
private String email;
private String password;

public UserMaster() {
}

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

public void setUserId(long userId) {
    this.userId = userId;
}

@Column(name = "user_name", nullable = false, length = 20)
public String getUserName() {
    return this.userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

@Column(name = "first_name", nullable = false, length = 20)
public String getFirstName() {
    return this.firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

@Column(name = "second_name", nullable = false, length = 20)
public String getSecondName() {
    return this.secondName;
}

public void setSecondName(String secondName) {
    this.secondName = secondName;
}

@Column(name = "email", length = 50)
public String getEmail() {
    return this.email;
}

public void setEmail(String email) {
    this.email = email;
}

@Column(name = "password", length = 20)
public String getPassword() {
    return this.password;
}

public void setPassword(String password) {
    this.password = password;
}

}

在我的DAO课程中,我有以下代码。

public UserMaster getUserByUserName(String userName) {
    Criteria criteria = getSession().createCriteria(UserMaster.class); 
    criteria.add(Restrictions.eq("userName", userName));
    UserMaster userMaster = (UserMaster) criteria.uniqueResult();
    return userMaster;
}

但每当我执行此代码时,我都会遇到异常。

[WARN ] 2017-07-18 18:24:23.105 [http-bio-8181-exec-9] SqlExceptionHelper - SQL Error: 0, SQLState: 42703
[ERROR] 2017-07-18 18:24:23.110 [http-bio-8181-exec-9] SqlExceptionHelper - ERROR: column this_.user_id does not exist
  Position: 8 org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: org.postgresql.util.PSQLException: ERROR: column this_.user_id does not exist
  Position: 8
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2412)

我做错了什么? Hibernate版本是5.1.8.Final。 Spring版本是4.3.5。

我打开了hibernate的show sql属性,下面的sql正在生成。

Hibernate: 
select
    this_.user_id as user_id1_0_0_,
    this_.email as email4_0_0_,
    this_.first_name as first_na5_0_0_,
    this_.password as password6_0_0_,
    this_.second_name as second_n7_0_0_,
    this_.user_name as user_nam9_0_0_ 
from
    public.appusermaster this_ 
where
    this_.user_name=?
[WARN ] 2017-07-18 19:22:40.797 [http-bio-8181-exec-12] SqlExceptionHelper - SQL Error: 0, SQLState: 42703
[ERROR] 2017-07-18 19:22:40.814 [http-bio-8181-exec-12] SqlExceptionHelper -     ERROR: column this_.user_id does not exist
  Position: 8

1 个答案:

答案 0 :(得分:0)

日志显示的生成的SQL是从名为“usermaster”而不是“APPUSERMASTER”的表中选择记录

看起来它没有拿起@Table注释。如果您更改了java中表的名称,则可能是您的类文件已过期且仍引用旧名称。