gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:37:16 28 4
gpt4 key购买 nike

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

关于java - 从 javax.persistence.Query 构建 org.eclipse.persistence.queries.ReportQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18485524/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com