从javax.persistence.Query构建org.eclipse.persistence.queries.ReportQuery

时间:2013-08-28 10:35:01

标签: java jpa eclipselink

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

1 个答案:

答案 0 :(得分:3)

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

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