JPA @NamedQuery与子查询相关

时间:2012-07-11 12:34:58

标签: jpa jpa-2.0 jpql named-query

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

感谢提前。

1 个答案:

答案 0 :(得分:7)

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

改为使用@NamedNativeQuery