org.postgresql.util.PSQLException:錯誤:列user0_.id不存在–Hibernate


org.postgresql.util.PSQLException:錯誤:列user0_.id不存在 - Hibernate(org.postgresql.util.PSQLException: ERROR: column user0_.id does not exist - Hibernate)

 3025      IT屋

我有一個使用hibernate映射到postgres數據庫的模型類。我的模型類是:



  @Entity 
@Table(name ="USER")
public class用戶{

@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")
私有時間戳已創建;

public User(long id,String username,String email){
this.id = id;
this.username = username;
this.email = email;
}
}


我嘗試使用用戶名"adam"檢索用戶使用以下查詢:



  tx = session.beginTransaction(); 
TypedQuery< User> query = session.createQuery("FROM User u WHERE u.username =:username",User.class).setParameter("username","adam");
user = query.getSingleResult();


我得到一個例外情況:



  org.postgresql.util.PSQLException:錯誤:列user0_.id不存在


我的bash shell數據庫如下所示:



database



< p> hibernate如何將類屬性映射到表列?它是否僅基於 @Column(name ="username")匹配,還是根據數據類型和約束(例如唯一/自動增量)嘗試匹配?< / p>  
解決方案

解釋



它給你這個錯誤:



  org.postgresql.util.PSQLException:錯誤:列user0_.id不存在


因為當你創建一個數據庫PostgreSQL時,它會創建一個名為 public 的默認模式,所以當你沒有在實體中指定名稱,它將在公共模式中自動檢查,因為您收到此錯誤。



另一件事確認模式名稱是正確的,錯誤說:



 列user0_.id不存在


而不是:



 列myapp.user0_.id不存在
------- ^ ---- ^


確認查詢使用公共架構而不是真正的架構myapp






解決方案



要解決此問題,您必須指定架構名稱,如下所示:



  @Table(name ="USER",schema ="myapp")





建議



不要在PostgreSQL中使用表格或列名稱中的大寫字母。



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


用戶名稱是PostgreSQL中的保留關鍵字ta a 看看


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

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:

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?

解決方案

Explication

It gives you this error :

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

Because when you create a database PostgreSQL it create a default schema named public, so when you don't specify the name in the Entity, it will check automatically in the public schema, for that you get this error.

Another thing confirm that the schema name is correct, the error said :

column user0_.id does not exist

and not :

column myapp.user0_.id does not exist
-------^----^

which confirm that the query use public schema and not the real schema myapp


Solution

To solve this problem you have to specify the name of schema like this :

@Table(name="USER", schema = "myapp")

Advice

don't use uppercase letter in the name of tables or columns in PostgreSQL.

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

and user name is reserved keyword in PostgreSQL ta a look at

本文地址:IT屋 » org.postgresql.util.PSQLException:錯誤:列user0_.id不存在 - Hibernate


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM