简体   繁体   English

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

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

I have a model class that is mapped to a postgres database using hibernate. My model class is:我有一个 model class 使用 hibernate 映射到 postgres 数据库。我的 model class 是:

@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 的数据库如下所示:

数据库

How does hibernate map class attributes to table columns? hibernate map class 是如何归属于表列的? 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")进行匹配,还是也尝试基于数据类型和约束(例如唯一/自动递增)进行匹配?

Solution解决方案

In PostgreSQL you 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 Entity then Hibernate will check automatically in the public schema.因为当您在PostgreSQL 中创建数据库时,它会创建一个名为public的默认架构,所以当您没有在Entity指定名称时,Hibernate 将自动检查公共架构。


Good practices良好做法

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

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

+----------+-----------+----------+-----------+---------+
| Key Word |PostgreSQL |SQL:2003  | SQL:1999  | SQL-92  |
+----------+-----------+----------+-----------+---------+
|  ....        ....       ....       ....       ....    |
+----------+-----------+----------+-----------+---------+
| USER     |  reserved |reserved  | reserved  | reserved|
+----------+-----------+----------+-----------+---------+
  1. to difference between Dto and Entity its good practice to use Entity in the end of the name of your Entity for example UserEntity为了区分DtoEntity在实体名称的末尾使用 Entity 的良好做法,例如UserEntity

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 letters in 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 generate the tables from entity classes or vice-versa.当您必须从实体类自动生成表或反之亦然的情况下,此约定变得更加重要。

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 管理员视图

在此处输入图片说明

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/

I obtained using general names like user are 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 User to Sessionxuser (and renaming the datatable from user to sessionxuser to fix this issue.我所做的只是将实体从User重命名为Sessionxuser (并将数据表从user重命名为sessionxuser以解决此问题。

Schema was still public . Schema 仍然是public

Since pre- or postfix some names like mycoolappuser or usermycoolapp to avoid troubles like this.由于mycoolappuser或后缀一些名称,如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 user is preserved for PostgreSQL , SQL:2003 , SQL:1999 and SQL-92 .在这种情况下, user被保留用于PostgreSQLSQL:2003SQL:1999SQL-92

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

In your POJO class Id Field.在您的 POJO 类 Id 字段中。 This thing solved my error.这件事解决了我的错误。

尝试从 pg 管理控制台drop table schema_name.table_namedrop table schema_name.table_name )并确保您的实体类正确注释。例如实体类上的@Table(name = "table_name", schema = "schema_name")

In addition to all previous correct answers, I'd like to say that the positioning of annotations @Column and @GeneratedValue also matters.除了之前所有的正确答案之外,我想说 @Column 和 @GeneratedValue 注释的定位也很重要。 You want to have both these annotations above either the specific field or getter method, but not separately ( not one annotation above the getter and the other above the field).您希望在特定字段或 getter 方法上方同时拥有这两个注释,但不能分开(不是一个注释位于 getter 上方,另一个注释位于字段上方)。 It worked for me at least.至少对我有用。

I fixed the issue by altering the column name from nameLikeThis to name_like_this我通过将列名从nameLikeThis更改为name_like_this来解决这个问题

ALTER TABLE table RENAME nameLikeThis to name_like_this;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.postgresql.util.PSQLException:错误:列 systementi0_.id 不存在 - Hibernate、PostgreSql - org.postgresql.util.PSQLException: Error: column systementi0_.id does not exist - Hibernate, PostgreSql 引起:org.postgresql.util.PSQLException:错误:列 performanc3_.app_user_internal_user_id 不存在 - Caused by: org.postgresql.util.PSQLException: ERROR: column performanc3_.app_user_internal_user_id does not exist org.postgresql.util.PSQLException:错误:列“id”不存在 - Java Web 服务 - org.postgresql.util.PSQLException: ERROR: column "id" does not exist - Java Web Service org.postgresql.util.PSQLException:错误:关系“用户”不存在 - SpringBoot,Hibernate,Postgresql - org.postgresql.util.PSQLException: ERROR: relation "users" does not exist - SpringBoot, Hibernate, Postgresql org.postgresql.util.PSQLException:错误:关系“序列”不存在 - org.postgresql.util.PSQLException: ERROR: relation “sequence” does not exist org.postgresql.util.PSQLException:错误:关系“产品”不存在 - org.postgresql.util.PSQLException: ERROR: relation "products" does not exist org.postgresql.util.PSQLException:错误:列 column0_.pk 不存在 Position:8 - org.postgresql.util.PSQLException: ERROR: column column0_.pk does not exist Position: 8 org.postgresql.util.PSQLException:错误:关系“clienti_id_seq”不存在 - org.postgresql.util.PSQLException: ERROR: the relation“clienti_id_seq” does not exist 错误:org.postgresql.util.PSQLException:错误:列 userinfo0_.dtype 不存在 - ERROR:org.postgresql.util.PSQLException:ERROR: column userinfo0_.dtype does not exist org.postgresql.util.PSQLException:错误:关系“app_user”不存在 - org.postgresql.util.PSQLException: ERROR: relation "app_user" does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM