1

我正在尝试创建一个@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 是否支持相关子查询?如果是这样,我如何创建一个将执行这种查询的命名查询?

提前致谢。

4

1 回答 1

7

@NamedQuery不支持 SQL,它只支持 JPQL。您的查询显然不是 JQPL,因为它使用特定于数据库的语法,例如 ROWNUMBER()。此外,在 JPQL 中,子查询仅在 WHERE 和 HAVING 子句中受支持。

请改用@NamedNativeQuery

于 2012-07-11T12:58:07.513 回答