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

后端 未结 6 2172
野性不改
野性不改 2021-02-05 02:47

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         


        
6条回答
  •  暖寄归人
    2021-02-05 03:24

    Solution

    In PostgreSQL you have to specify the name of schema like so :

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


    Good practices

    1. Don't use Upper letters in the name of 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")
                 ^^^^^^^^^^             ^^^^^^^^^^^
    
    1. the keyword USER is reserved keyword in PostgreSQL take a look at

    +----------+-----------+----------+-----------+---------+
    | 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

提交回复
热议问题