2012-03-15 33 views
3

我有一個執行HQL子查詢內情況下語句時的問題。休眠子查詢內情況下,當語句

這裏是我的代碼:

select case when a.entityId is null then 'invalid' else 
    (select b.entityName from tblName b where b.entityId =a.entityId) 
    end from tblEntity a 

感謝您的幫助!

行..這裏是完整的堆棧跟蹤

ERROR org.hibernate.hql.PARSER - <AST>:0:0: unexpected AST node: query 
Exception in thread "Thread-5" org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: query [select case when a.entityId is null then 'invalid' else 
     (select b.entityName from tblName b where b.entityId =a.entityId) 
     end from tblEntity a] 
    at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54) 
    at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47) 
    at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82) 
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:261) 
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185) 
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136) 
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101) 
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80) 
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94) 
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156) 
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135) 
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1651) 
+1

它給了什麼錯誤? – ManuPK 2012-03-15 08:32:24

+0

org.hibernate.hql.ast.QuerySyntaxException:意外的AST節點 – 2012-03-16 00:03:58

+0

仍然沒有線索!編輯問題,並添加**完整的堆棧跟蹤**! – ManuPK 2012-03-16 04:03:32

回答

2

此功能不處於休眠支持。 但確實存在解決方法。 在數據庫中創建一個視圖,然後使用HQL通過java獲取結果。

創建或替換力視圖VW_ENTITY_VALUE(entityValue) 作爲 選擇情況下,當a.entityId是空值,則 '無效' 其他 (從tblName b。選擇所需b.entityName其中b.entityId = a.entityId) 從端tblEntity a;

從HQL的文檔: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-subqueries

14.13。子查詢

對於支持子查詢的數據庫,Hibernate支持查詢中的子查詢。子查詢必須用圓括號包圍(通常由SQL聚合函數調用)。即使相關的子查詢(引用外部查詢中的別名的子查詢)也是允許的。

從貓作爲fatcat 其中fatcat.weight>從爲DomesticCat( SELECT AVG(cat.weight)從貓DomesticCat的 ) 如貓 其中cat.name =從名稱一些( 選擇name.nickName如名稱 ) 從貓如貓 其中不存在( 從貓大副其中mate.mate =貓 ) 從作爲DomesticCat的貓 其中cat.name未在從名稱( 選擇name.nickName姓名 ) 選擇cat.id,(sel ect max(kit.weight)from cat.kitten kit) from cat作爲cat 請注意,HQL子查詢只能出現在select或where子句中。