我正在尝试使用单个命名查询来获取一组结果并计算可用数据量。我有一个命名的 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。