亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Hibernate HQL select 與 where order by 查詢不同

Hibernate HQL select 與 where order by 查詢不同

慕尼黑5688855 2023-11-10 17:02:22
我正在更新一個項目,以明確了解客戶,以前假設客戶,因為我們只有一個......我的問題是向 HQL 查詢添加 where 子句。我的出發點是這個查詢:  public static final String SELECT_DISTINCT_STORES =        "select DISTINCT e.storeNum, e.city, e.state from BoEngagement e order by e.storeNum";我想添加一個where e.customer_fk = :customer_fk子句,但每次添加 where 子句時,我都會收到各種org.hibernate.hql.internal.ast.QuerySyntaxException錯誤,除非我取出distinct關鍵字,但我不相信查詢會給出我所期望的結果。這有效:   "select e.storeNum, e.city, e.state from BoEngagement e WHERE e.customer_fk = :customer_fk";而且,如果我要簡化查詢那么多,它確實應該是"select e from BoEngagement e WHERE e.customer_fk = :customer_fk";然而,正如我所說,我不相信刪除關鍵字distinct是我想要做的。以下是我嘗試過的一些事情:   "select DISTINCT e.storeNum, e.city, e.state FROM BoEngagement e WHERE e.customer_fk = :customer_fk order by e.storeNum";給出這個錯誤[ERROR] 2019-10-18 15:10:03.449 [main] BsRetrieveDistinct - java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: FROM near line 1, column 94 [SELECT DISTINCT e.city, e.state from com.bh.radar.bo.BoEngagement e order by e.state, e.city FROM com.bh.radar.bo.BoEngagement e WHERE e.customer_fk = :customer_fk]java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: FROM near line 1, column 94 [SELECT DISTINCT e.city, e.state from com.bh.radar.bo.BoEngagement e order by e.state, e.city FROM com.bh.radar.bo.BoEngagement e WHERE e.customer_fk = :customer_fk]這個更復雜的版本   "select DISTINCT e.storeNum, e.city, e.state FROM BoEngagement e in " +        "(select g FROM BoEngagement g WHERE g.customer_fk = :customer_fk order by g.storeNum)";顯然我并沒有完全理解HQL和distinct關鍵字。我究竟做錯了什么?
查看完整描述

1 回答

?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

研究 EntityManager.createNativeQuery 方法,并提出這個有效的 PostgreSQL 查詢:

??public?static?final?String?SELECT_DISTINCT_STORES?=
?????????"SELECT?DISTINCT?ON(storeNum,?city,?state)?*?FROM?radar2.engagement?WHERE?customer_fk?=?:customer_fk?ORDER?BY?storeNum?asc,?city,?state";

我這樣使用它:

????results?=?em.createNativeQuery(SELECT_DISTINCT_CITY_STATES,?BoEngagement.class)
????????????????.setParameter("customer_fk",?customer_fk)
????????????????.getResultList();

現在,這個解決方案特定于 PostgreSQL,但這是我現在和無限期的未來使用的數據庫。


查看完整回答
反對 回復 2023-11-10
  • 1 回答
  • 0 關注
  • 314 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號