查询尚未正确定义。对于子查询和并行查询,请确保查询构建器始终位于左侧

时间:2016-05-26 10:32:54

标签: java jpa jpql criteria-api

我需要在dateFrom和dateTo之间每天创建一条记录。它应该是当天的第一个记录(换句话说,记录日期范围内每个日期的最小ID)。所以在结果中我需要这些记录的列表。 我试试这些代码:

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<CurrencyConvertionRate> cq = cb.createQuery(CurrencyConvertionRate.class);
    Root<CurrencyConvertionRate> ccrFrom = cq.from(CurrencyConvertionRate.class);
    cq.select(ccrFrom);

    Subquery<Long> subQuery = cq.subquery(Long.class);
    Root<CurrencyConvertionRate> from = subQuery.from(CurrencyConvertionRate.class);
    subQuery.select(cb.least(from.<Long>get("id")));
    subQuery.where(
            (cb.greaterThanOrEqualTo(from.<Timestamp>get("created"), dateFrom)),
            (cb.lessThanOrEqualTo(from.<Timestamp>get("created"), dateTo)),
            from.get("currencyCode").in(currencyList)
    );
    subQuery.groupBy(from.<Timestamp>get("created"));

    cq.where(cb.in(from.<Long>get("id")).value(subQuery));
    TypedQuery<CurrencyConvertionRate> typedQuery = entityManager.createQuery(cq);

并获得例外:

 org.eclipse.persistence.exceptions.QueryException:
 Exception Description: The query has not been defined correctly, the   expression builder is missing.  For sub and parallel queries ensure the queries builder is always on the left.
Query: ReadAllQuery(referenceClass=CurrencyConvertionRate )
    at org.eclipse.persistence.exceptions.QueryException.invalidBuilderInQuery(QueryException.java:683) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.expressions.SQLSelectStatement.appendFromClauseToWriter(SQLSelectStatement.java:537) ~[org.eclipse.persistence.core.jar:na]
    at org.eclipse.persistence.internal.expressions.SQLSelectStatement.printSQL(SQLSelectStatement.java:1621) ~[org.eclipse.persistence.core.jar:na]

Exception was thrown: javax.persistence.PersistenceException, Exception [EclipseLink-6121] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.QueryException
Exception Description: The query has not been defined correctly, the expression builder is missing.  For sub and parallel queries ensure the queries builder is always on the left.
Query: ReadAllQuery(referenceClass=CurrencyConvertionRate )
An exception has been thrown from an exception mapper class com.lpb.ecom.exception.mapper.RuntimeExceptionMapper.
java.lang.NullPointerException: null
    at com.lpb.ecom.exception.ExceptionHandler.process(ExceptionHandler.java:115) ~[ecom-lib.jar:na]
    at com.lpb.ecom.exception.ExceptionHandler.getErrorBean(ExceptionHandler.java:71) ~[ecom-lib.jar:na]
    at com.lpb.ecom.exception.mapper.RuntimeExceptionMapper.toResponse(RuntimeExceptionMapper.java:15) ~[ecom-lib.jar:na]
    at com.lpb.ecom.exception.mapper.RuntimeExceptionMapper.toResponse(RuntimeExceptionMapper.java:10) ~[ecom-lib.jar:na]
    at org.glassfish.jersey.server.ServerRuntime$Responder.mapException(ServerRuntime.java:396) [jersey-server.jar:na]
    at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:344) [jersey-server.jar:na]
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:230) [jersey-server.jar:na]
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) [jersey-common.jar:na]
    at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) [jersey-common.jar:na]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315) [jersey-common.jar:na]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297) [jersey-common.jar:na]
    at org.glassfish.jersey.internal.Errors.process(Errors.java:267) [jersey-common.jar:na]
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) [jersey-common.jar:na]
    at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:198) [jersey-server.jar:na]
    at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:946) [jersey-server.jar:na]
    at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:323) [jersey-container-servlet-core.jar:na]
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:372) [jersey-container-servlet-core.jar:na]
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:335) [jersey-container-servlet-core.jar:na]
    at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:218) [jersey-container-servlet-core.jar:na]
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) [web-core.jar:na]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) [web-core.jar:na]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) [web-core.jar:na]
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) [web-core.jar:na]
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) [web-core.jar:na]
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) [web-glue.jar:na]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) [web-core.jar:na]
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357) [web-core.jar:na]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260) [web-core.jar:na]
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188) [kernel.jar:na]
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564) [nucleus-grizzly-all.jar:na]
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544) [nucleus-grizzly-all.jar:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_05]

0 个答案:

没有答案