zoukankan      html  css  js  c++  java
  • 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

    为人:谦逊、激情、博学、审问、慎思、明辨、 笃行
    学问:纸上得来终觉浅,绝知此事要躬行
    为事:工欲善其事,必先利其器。
    态度:道阻且长,行则将至;行而不辍,未来可期
    转载请标注出处!
  • 相关阅读:
    获取连接无线路由客户机信息命令
    HTB进行流量控制方法
    exec函数族用法
    java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    struts2从请求取值的三种方式
    用jsp写的网页 怎么在传递参数时包含中文?
    Struts2使用DoubleSelect实现二级级联下拉框省份城市
    MySQL里主键与外键的关系
    查看struts2源码
    WIN7系统下,用笔记本发送WIFI信号让手机无线上网!
  • 原文地址:https://www.cnblogs.com/ios9/p/15498201.html
Copyright © 2011-2022 走看看