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
org.postgresql.util.PSQLException: ERROR: column user0_.id does not exist - Hibernate
提问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 数据库如下所示:
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 Entity
then Hibernate will check automatically in the publicschema.
因为当您在PostgreSQL 中创建数据库时,它会创建一个名为public的默认模式,因此当您未在其中指定名称时,Entity
Hibernate 将自动检查public模式。
Good practices
良好做法
- Don't use Upper letters in the name of
database
,schema
,tables
orcolumns
in PostgreSQL. Else you should to escape this names with quotes, and this can cause Syntax errors, so instead you can use :
- 不要在名称中使用字母上
database
,schema
,tables
或columns
在PostgreSQL的。否则你应该用引号转义这个名字,这可能会导致语法错误,所以你可以使用:
@Table(name="table_name", schema = "schame_name")
^^^^^^^^^^ ^^^^^^^^^^^
- the keyword USERis reserved keyword in PostgreSQLtake a look at
- 关键字USER是PostgreSQL 中的保留关键字看看
+----------+-----------+----------+-----------+---------+
| Key Word |PostgreSQL |SQL:2003 | SQL:1999 | SQL-92 |
+----------+-----------+----------+-----------+---------+
| .... .... .... .... .... |
+----------+-----------+----------+-----------+---------+
| USER | reserved |reserved | reserved | reserved|
+----------+-----------+----------+-----------+---------+
- to difference between Dtoand Entityits good practice to use Entity in the end of the name of your Entity for example
UserEntity
- 区分Dto和Entity的好习惯,例如在实体名称的末尾使用 Entity
UserEntity
回答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 管理员视图
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 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
toSessionxuser
重命名(并将数据表从user
to重命名sessionxuser
以解决此问题。
Schema was still public
.
架构仍然是public
。
Since pre- or postfix some names like mycoolappuser
or usermycoolapp
to avoid troubles like this.
由于前缀或后缀一些名称,例如mycoolappuser
或usermycoolapp
避免这样的麻烦。
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
被保存PostgreSQL
,SQL:2003
,SQL:1999
和SQL-92
。