/**PageBeginHtml Block Begin **/ /***自定义返回顶部小火箭***/ /*生成博客目录的JS 开始*/ /*生成博客目录的JS 结束*/

org.postgresql.util.PSQLException:错误:列user0_.id不存在–Hibernate

org.postgresql.util.PSQLException:错误:列user0_.id不存在 - Hibernate(org.postgresql.util.PSQLException: ERROR: column user0_.id does not exist - Hibernate)

 3025   IT屋

我有一个使用hibernate映射到postgres数据库的模型类。我的模型类是:



  @Entity 
@Table(name ="USER")
public class用户{

@Id
@GeneratedValue
@Column(name ="id")
private long id;

@Column(name ="username",unique = true)
private String username;

@Column(name ="email")
private String email;

@Column(name ="created")
私有时间戳已创建;

public User(long id,String username,String email){
this.id = id;
this.username = username;
this.email = email;
}
}


我尝试使用用户名"adam"检索用户使用以下查询:



  tx = session.beginTransaction(); 
TypedQuery< User> query = session.createQuery("FROM User u WHERE u.username =:username",User.class).setParameter("username","adam");
user = query.getSingleResult();


我得到一个例外情况:



  org.postgresql.util.PSQLException:错误:列user0_.id不存在


我的bash shell数据库如下所示:



database



< p> hibernate如何将类属性映射到表列?它是否仅基于 @Column(name ="username")匹配,还是根据数据类型和约束(例如唯一/自动增量)尝试匹配?< / p> 
解决方案

解释



它给你这个错误:



  org.postgresql.util.PSQLException:错误:列user0_.id不存在


因为当你创建一个数据库PostgreSQL时,它会创建一个名为 public 的默认模式,所以当你没有在实体中指定名称,它将在公共模式中自动检查,因为您收到此错误。



另一件事确认模式名称是正确的,错误说:



 列user0_.id不存在


而不是:



 列myapp.user0_.id不存在
------- ^ ---- ^


确认查询使用公共架构而不是真正的架构myapp






解决方案



要解决此问题,您必须指定架构名称,如下所示:



  @Table(name ="USER",schema ="myapp")





建议



不要在PostgreSQL中使用表格或列名称中的大写字母。



  @Table(name ="user",schema ="myapp")


用户名称是PostgreSQL中的保留关键字ta a 看看


I have a model class that is mapped to a postgres database using hibernate. My model class is:

@Entity
@Table(name="USER")
public class User {

    @Id 
    @GeneratedValue
    @Column(name="id")
    private long id;

    @Column(name="username", unique=true)
    private String username;

    @Column(name="email")
    private String email;

    @Column(name="created")
    private Timestamp created;

    public User(long id, String username, String email) {
        this.id = id;
        this.username = username;
        this.email = email;
    }
}

I try to retrieve the user with username "adam" using the below query:

tx = session.beginTransaction();
TypedQuery<User> query = session.createQuery("FROM User u WHERE u.username = :username", User.class).setParameter("username", "adam");
user = query.getSingleResult();

I get an exception that says:

org.postgresql.util.PSQLException: ERROR: column user0_.id does not exist

My database from bash shell looks like:

database

How does hibernate map class attributes to table columns? Does it match based on the @Column(name="username") only or does it also try to match based on datatypes and constraints such as unique/auto-increment?

解决方案

Explication

It gives you this error :

org.postgresql.util.PSQLException: ERROR: column user0_.id does not exist

Because when you create a database PostgreSQL it create a default schema named public, so when you don't specify the name in the Entity, it will check automatically in the public schema, for that you get this error.

Another thing confirm that the schema name is correct, the error said :

column user0_.id does not exist

and not :

column myapp.user0_.id does not exist
-------^----^

which confirm that the query use public schema and not the real schema myapp


Solution

To solve this problem you have to specify the name of schema like this :

@Table(name="USER", schema = "myapp")

Advice

don't use uppercase letter in the name of tables or columns in PostgreSQL.

@Table(name="user", schema = "myapp")

and user name is reserved keyword in PostgreSQL ta a look at

本文地址:IT屋 » org.postgresql.util.PSQLException:错误:列user0_.id不存在 - Hibernate

posted @ 2021-11-02 11:56  一品堂.技术学习笔记  阅读(1351)  评论(0编辑  收藏  举报