2012-07-11 187 views
1

我試圖創建一個@NamedQuery將包含以下相關子查詢:JPA @NamedQuery相關子查詢

@NamedQuery(name = "Drcomments.findByOrderDesc", query = " SELECT a FROM (SELECT ROWNUMBER() OVER(ORDER BY d.drcommentsPK.commentSecondaryCode DESC) AS EL_ROWNM , d.drcommentsPK.commentSecondaryCode , d.commentSecondaryCodeDescription FROM Drcomments d WHERE d.drcommentsPK.commentPrimaryCode = 1) a WHERE a.EL_ROWNM < 4 and a.EL_ROWNM > 0 ")})  

的問題是,我得到這個錯誤:

"Exception [EclipseLink-8025] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.JPQLException 
Exception Description: Syntax error parsing the query [Drcomments.findByOrderDesc: SELECT a FROM (SELECT ROWNUMBER() OVER(ORDER BY d.drcommentsPK.commentSecondaryCode DESC) AS EL_ROWNM , d.drcommentsPK.commentSecondaryCode , d.commentSecondaryCodeDescription FROM Drcomments d WHERE d.drcommentsPK.commentPrimaryCode = 1) a WHERE a.EL_ROWNM < 4 and a.EL_ROWNM > 0 ], line 1, column 15: unexpected token [(]. 
Internal Exception: org.eclipse.persistence.internal.jpa.parsing.jpql.InvalidIdentifierException" 

不@NamedQuery支持相關的subquerys?如果是這樣, 如何創建一個將執行此類查詢的命名查詢?

感謝提前。

回答

7

@NamedQuery不支持SQL,它只支持JPQL。您的查詢顯然不是JQPL,因爲它使用數據庫特定的語法,如ROWNUMBER()。此外,JPQL子查詢僅在WHERE和HAVING子句中受支持。

改爲使用@NamedNativeQuery