我正在尝试使用单个命名查询来获取一组结果并计算可用数据量。我有一个命名的JPQL查询:

@NamedQuery(name = "query.all.absences.by.name", query = "SELECT a FROM Absence a WHERE a.name = :name")


当我获得缺勤时,我将像这样调用查询:

final Query q = em.createNamedQuery("query.all.absences.by.name");
q.setParameter("name","aRandomAbsenceName");
//maxResults and firstResult are coming as parameters, are computed in another method
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);


q.getResultList()为我提供正确的结果集。到现在为止还挺好。

现在,我要做的是使用相同的命名查询来执行计数查询。
我正在执行以下操作:

final Query q = em.createNamedQuery("query.all.absences.by.name");
q.setParameter("name","aRandomAbsenceName");
q.setHint(QueryHints.QUERY_TYPE, "org.eclipse.persistence.queries.ReportQuery");
final ReportQuery test = JpaHelper.getReportQuery(q);
test.addCount();
test.setShouldReturnWithoutReportQueryResult(true);


如果我的命名查询没有参数,则此方法效果很好。但是使用参数时,如果我执行上面定义的ReportQuery,则有以下异常:

Exception [EclipseLink-6094] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.QueryException
Exception Description: The parameter name [name] in the query's selection criteria does not match any parameter name defined in the query.
Query: ReportQuery(referenceClass=AbsenceCategory sql="SELECT COUNT(CATEGORY_KEY) FROM ABSENCE_CATEGORY WHERE (CATEGORY_KEY = ?)")
    at org.eclipse.persistence.exceptions.QueryException.parameterNameMismatch(QueryException.java:1063)
    at org.eclipse.persistence.internal.expressions.ParameterExpression.getValue(ParameterExpression.java:275)
    at org.eclipse.persistence.internal.databaseaccess.DatabaseCall.translate(DatabaseCall.java:1000)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:206)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeCall(DatasourceCallQueryMechanism.java:193)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.executeSelectCall(DatasourceCallQueryMechanism.java:264)
    at org.eclipse.persistence.internal.queries.DatasourceCallQueryMechanism.selectAllRows(DatasourceCallQueryMechanism.java:646)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllRowsFromTable(ExpressionQueryMechanism.java:2611)
    at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.selectAllReportQueryRows(ExpressionQueryMechanism.java:2554)
    at org.eclipse.persistence.queries.ReportQuery.executeDatabaseQuery(ReportQuery.java:846)
    at org.eclipse.persistence.queries.DatabaseQuery.execute(DatabaseQuery.java:844)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.execute(ObjectLevelReadQuery.java:1040)
    at org.eclipse.persistence.queries.ReadAllQuery.execute(ReadAllQuery.java:392)
    at org.eclipse.persistence.queries.ObjectLevelReadQuery.executeInUnitOfWork(ObjectLevelReadQuery.java:1128)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.internalExecuteQuery(UnitOfWorkImpl.java:2871)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1516)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1498)
    at org.eclipse.persistence.internal.sessions.AbstractSession.executeQuery(AbstractSession.java:1449)


我还能如何将参数设置为ReportQuery?

我正在使用EclipseLink 2.0。

最佳答案

您如何执行ReportQuery?您需要调用Session.executeQuery(query,arguments)并传递参数列表(顺序与query.getArguments()相同)。

您可能还可以使用JpaEntityManager createQuery(DatabaseQuery)API将ReportQuery转换回JPA查询,然后就可以以JPA方式设置参数。

10-06 01:07