簡體   English   中英

如何在列名中使用下划線執行JPQL查詢?

[英]How do I do a JPQL query with an underscore in the column name?

我收到以下JPQL查詢錯誤:

@NamedQuery (name = "Customer.getById", query = 
"SELECT o 
FROM bub.Customer o 
WHERE o.user_id = :myid")

[bub.Customer是@Entity名稱]

這是我收到的錯誤消息的摘錄:

org.hibernate.HibernateException: 
Errors in named queries: 
Customer.getById\n 
Caused by: org.hibernate.HibernateException: 
Errors in named queries: Customer.getById

當我刪除WHERE子句時,Wildfly允許我部署我的Web應用程序,所以我知道我的WHERE子句有問題。 特別是因為我的Customer表中的列名是user_id,所以我認為JPQL中的下划線(_)可能存在問題。 我已經嘗試將WHERE子句更改為“WHERE o.userId =:myid”,但這也不起作用。

我如何修復WHERE子句,以便我的網站部署並仍然以正確的方式工作?

編輯:相關的方法是這樣的:

public static Customer getById (final EntityManager em, final long id)
{
    return em.createNamedQuery ("Customer.getById", Customer.class).setParameter ("myid", id).getSingleResult ();
}

我不認為這是問題所在。

編輯2:事實證明這是問題:

@ManyToOne (fetch = FetchType.LAZY)
@JoinColumn (name = "user_id")
private User         user;

我最終將JPQL查詢更改為此,現在它正在工作:

@NamedQuery (name = "Customer.getById", query = 
"SELECT o 
FROM bub.Customer o 
WHERE o.user = :myid")

在JPQL中,您不使用列名,而是使用屬性名稱(除非您注釋其他內容)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM