2012-07-09 57 views
0

任何人都可以解釋爲什麼會拋出JPQL異常嗎?jpql按尺寸排序錯誤

Query q = em.createQuery("select object(o) from SiteMember as o order by size(o.memberThread) desc"); 

memberThread refer a list. 

回答

0

很可能是因爲您使用的JPA實現(也是數據庫供應商可能很重要),它沒有針對此查詢的供應商擴展。根據JPA 2.0以下類型的值可以在ORDER BY

1.A state_field_path_expression that evaluates to an orderable state field of an 
    entity or embeddable class abstract schema type designated in the SELECT clause 
    by one of the following: 
• a general_identification_variable 
• a single_valued_object_path_expression 

2.A state_field_path_expression that evaluates to the same state field of the same 
    entity or embeddable abstract schema type as a state_field_path_expression in the 
    SELECT clause 

3.A result_variable that refers to an orderable item in the SELECT clause for which 
the same result_variable has been specified. This may be the result of an 
aggregate_expression, a scalar_expression, or a state_field_path_expression in the 
SELECT clause. 

您的查詢工作,例如與EclipseLink的(2.3.2)和MySQL。

+0

我使用Toplink和這段代碼工作正常「從MemberThread選擇對象(o)作爲o其中size(o.replys)='0'order by o.id desc」.any解決方案請嗎? – bews99 2012-07-09 07:37:23

+0

那一個是有效的JPQL查詢。問題在於ORDER BY與非法價值相結合。很可能只是查詢的排序結果很好,至少沒有可移植性問題。如果這不是選項,您可以嘗試遵循(不保證工作):將'size(o.memberThread)'作爲ord'添加到SELECT,按ord排序,並按SiteMember中的字段進行分組。然後它會變得醜陋,因爲結果還包含僅用於ORDER BY的屬性。 – 2012-07-09 08:04:16

+0

感謝您的詳細回覆。 :D – bews99 2012-07-09 10:05:05