Java org.postgresql.util.PSQLException:错误:列 user0_.id 不存在 - 休眠

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45782327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:10:29  来源:igfitidea点击:

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

javapostgresqlhibernatejpaentity

提问by kovac

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

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

@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:

我尝试使用以下查询检索用户名为“adam”的用户:

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:

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

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?

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

采纳答案by YCF_L

Solution

解决方案

In PostgreSQLyou have to specify the name of schema like so :

PostgreSQL 中,您必须像这样指定模式的名称:

@Table(name="table_name", schema = "myapp")
                          ^^^^^^^^^^^^^^^^

Long Story

很长的故事

you got this error :

你有这个错误:

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

because when you create a database in PostgreSQL, it create a default schema named public, so when you don't specify the name in the Entitythen Hibernate will check automatically in the publicschema.

因为当您在PostgreSQL 中创建数据库时,它会创建一个名为public的默认模式,因此当您未在其中指定名称时,EntityHibernate 将自动检查public模式。



Good practices

良好做法

  1. Don't use Upper letters in the name of database, schema, tablesor columnsin PostgreSQL. Else you should to escape this names with quotes, and this can cause Syntax errors, so instead you can use :
  1. 不要在名称中使用字母上databaseschematablescolumnsPostgreSQL的。否则你应该用引号转义这个名字,这可能会导致语法错误,所以你可以使用:

@Table(name="table_name", schema = "schame_name")
             ^^^^^^^^^^             ^^^^^^^^^^^
  1. the keyword USERis reserved keyword in PostgreSQLtake a look at
  1. 关键字USERPostgreSQL 中的保留关键字看看

+----------+-----------+----------+-----------+---------+
| Key Word |PostgreSQL |SQL:2003  | SQL:1999  | SQL-92  |
+----------+-----------+----------+-----------+---------+
|  ....        ....       ....       ....       ....    |
+----------+-----------+----------+-----------+---------+
| USER     |  reserved |reserved  | reserved  | reserved|
+----------+-----------+----------+-----------+---------+
  1. to difference between Dtoand Entityits good practice to use Entity in the end of the name of your Entity for example UserEntity
  1. 区分DtoEntity的好习惯,例如在实体名称的末尾使用 EntityUserEntity

回答by Vikash Kumar

For people getting this exception ,In postgres Whenever you write an Entity Class try to associate it with the correct schema (where your table is present), like this:

对于遇到此异常的人,在 postgres 中,每当您编写实体类时,请尝试将其与正确的架构(您的表所在的位置)相关联,如下所示:

@Entity
@Table(name = "user", schema = "users_details")
public class User implements Serializable{

    @Column(name = "id")
    Long id;    //long is not recommended

   // Other data
}

As @YCF_L has said Don't use Upper_case lettersin a table name or column name otherwise you will get this exception.

正如@YCF_L 所说,不要在表名或列名中使用大写字母,否则您将收到此异常。

This convention becomes more important when their is a scenario where you have to auto generatethe tables from entity classes or vice-versa.

当您必须从实体类自动生成表或反之亦然的情况下,此约定变得更加重要。

回答by Raj Shukla

Use: @GeneratedValue(strategy = GenerationType.IDENTITY)

使用:@GeneratedValue(strategy = GenerationType.IDENTITY)

In your POJO class Id Field. This thing solved my error.

在您的 POJO 类 Id 字段中。这件事解决了我的错误。

回答by Cebu CM Solutions

Should add schema name on the Entity class. For this example, when the schema name is public

应该在实体类上添加模式名称。对于此示例,当架构名称为 public 时

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

See the PostgreSQL Admin view below

请参阅下面的 PostgreSQL 管理员视图

enter image description here

在此处输入图片说明

See here for more about SpringBoot Java and Postgre SQL connectivity: https://cmsoftwaretech.wordpress.com/2020/04/25/springboot-thymleaf-using-postgresql/

有关 SpringBoot Java 和 Postgre SQL 连接的更多信息,请参见此处:https://cmsoftwaretech.wordpress.com/2020/04/25/springboot-thymleaf-using-postgresql/

回答by Daniel Gsch?sser

I obtained using general names like userare making troubles in the app.

我使用通用名称获得,例如user在应用程序中制造麻烦。

I got the same issue as reported here with the following simple entity.

我使用以下简单实体遇到了与此处报告的相同的问题。

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

@Entity
public class User implements Serializable {

    private static final long serialVersionUID = 6843302791607583447L;

    @Id
    @SequenceGenerator(name = "user_id_seq", sequenceName = "user_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_id_seq")
    private Long id;

    @Column
    private String name;

    @Column
    private String password;

    @Override
    public String toString() {
        return name;
    }

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

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

}

All i did was renaming the entity from Userto Sessionxuser(and renaming the datatable from userto sessionxuserto fix this issue.

我所做的只是将实体从UsertoSessionxuser重命名(并将数据表从userto重命名sessionxuser以解决此问题。

Schema was still public.

架构仍然是public

Since pre- or postfix some names like mycoolappuseror usermycoolappto avoid troubles like this.

由于前缀或后缀一些名称,例如mycoolappuserusermycoolapp避免这样的麻烦。

Find below a list with reserved keywords and literals preventing using as table, column, and further customized names.

在下面找到一个包含禁止用作表、列和进一步自定义名称的保留关键字和文字的列表。

https://www.postgresql.org/docs/8.1/sql-keywords-appendix.html

https://www.postgresql.org/docs/8.1/sql-keywords-appendix.html

In this case useris preserved for PostgreSQL, SQL:2003, SQL:1999and SQL-92.

在这种情况下user被保存PostgreSQLSQL:2003SQL:1999SQL-92