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

查看:3323
本文介绍了org.postgresql.util.PSQLException:错误:列user0_.id不存在 - Hibernate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
    }
}

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

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();

我得到一个例外情况:

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

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

My database from bash shell looks like:

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

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

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

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

而不是:

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

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

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

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

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

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






建议



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


Advice

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

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

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

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

这篇关于org.postgresql.util.PSQLException:错误:列user0_.id不存在 - Hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆