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
@G
In PostgreSQL you have to specify the name of schema like so :
@Table(name="table_name", schema = "myapp")
^^^^^^^^^^^^^^^^
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.
database
, schema
, tables
or columns
in PostgreSQL. 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")
^^^^^^^^^^ ^^^^^^^^^^^
+----------+-----------+----------+-----------+---------+
| Key Word |PostgreSQL |SQL:2003 | SQL:1999 | SQL-92 |
+----------+-----------+----------+-----------+---------+
| .... .... .... .... .... |
+----------+-----------+----------+-----------+---------+
| USER | reserved |reserved | reserved | reserved|
+----------+-----------+----------+-----------+---------+
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:
@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.
This convention becomes more important when their is a scenario where you have to auto generate the tables from entity classes or vice-versa.
Try Dropping the table from pg admin console (drop table schema_name.table_name
)and make sure your entity class is proper annotated.For example @Table(name = "table_name", schema = "schema_name")
on entity class
Should add schema name on the Entity class. For this example, when the schema name is public
@Table(name = "user", schema = "public")
See the PostgreSQL Admin view below
See here for more about SpringBoot Java and Postgre SQL connectivity: https://cmsoftwaretech.wordpress.com/2020/04/25/springboot-thymleaf-using-postgresql/
I obtained using general names like user
are making troubles in the app.
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.
Schema was still public
.
Since pre- or postfix some names like mycoolappuser
or usermycoolapp
to avoid troubles like this.
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
In this case user
is preserved for PostgreSQL
, SQL:2003
, SQL:1999
and SQL-92
.
Use: @GeneratedValue(strategy = GenerationType.IDENTITY)
In your POJO class Id Field. This thing solved my error.