CriteriaQuery 多选 - 运行时构造函数错误

Posted

技术标签:

【中文标题】CriteriaQuery 多选 - 运行时构造函数错误【英文标题】:CriteriaQuery multiselect - Runtime constructor error 【发布时间】:2015-11-06 17:57:50 【问题描述】:

我正在使用 CriteriaQuery 创建具有特定类类型的 TypedQuery。 我只需要类类型中的几列。 我正在通过多选方法选择这些列,但我在下面的 RuntimeException 中苦苦挣扎。 这是说系统无法罚款构造函数。 但是,我创建了构造函数。

请给我关于这个问题的建议。

使用条件创建查询。

CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<VwLocationBrief> criteriaQuery = criteriaBuilder.createQuery(VwLocationBrief.class);

Root<VwLocationBrief> root = (Root<VwLocationBrief>) criteriaQuery.from(VwLocati onBrief.class);
CriteriaHelper<VwLocationBrief> criteriaHelper = criteriaHelperFactory.newCriteriaHelper(criteriaBuilder, root);
criteriaHelper.ignoreCaseLike(CONTACT_NAME, locationAdvancedSearchCriteria.getContactName())
        .ignoreCaseEq(CONTACT_EMAIL, locationAdvancedSearchCriteria.getEmailaddress())
        .ignoreCaseLike(LOCATION_NAME2, locationAdvancedSearchCriteria.getLocationName())
        .ignoreCaseLike(UNIT_NAME, locationAdvancedSearchCriteria.getUnitName())
        .ignoreCaseLike(LOCATION_CODE, locationAdvancedSearchCriteria.getLocationCode())
        .ignoreCaseAndSpaceSuffixLike(POSTCODE, locationAdvancedSearchCriteria.getPostcode() != null ? locationAdvancedSearchCriteria.getPostcode().replaceAll(" ", "") : null)
        .orderBy(LOCATION_NAME2, CriteriaHelper.ASCENDING);

//Here are the columns that I want to select
**criteriaQuery.multiselect(
        root.get(ADDRESS_LINE1),
        root.get(ADDRESS_LINE2),
        root.get(ADDRESS_LINE3),
        root.get(ADDRESS_LINE5),
        root.get("businessName"),
        root.get(BRN_PARAM),        
        root.get(LOCATION_CODE),
        root.get("locationId"),
        root.get(LOCATION_NAME),
        root.get("mainLocation"),   
        root.get(POST_TOWN),
        root.get(POSTCODE))
.distinct(true);**

criteriaQuery.where(criteriaHelper.getPredicates());
if(type != null && type.equals("list")) 
    criteriaQuery.orderBy(criteriaHelper.getOrdering());

这里是构造函数。

public VwLocationBrief(String addressLine1, String addressLine2,
            String addressLine3, String addressLine5, String businessName,
            long brn, String locationCode, long locationId,
            String locationName, boolean mainLocation, String postTown,
            String postcode) 
        this.addressLine1 = addressLine1;
        this.addressLine2 = addressLine2;
        this.addressLine3 = addressLine3;
        this.addressLine5 = addressLine5;
        this.businessName = businessName;
        this.brn = brn;
        this.locationCode = locationCode;
        this.locationId = locationId;
        this.locationName = locationName;
        this.mainLocation = mainLocation;
        this.postTown = postTown;
        this.postcode = postcode;
    

这是我遇到的例外情况。

<openjpa-2.2.0-r422266:1244990 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: Failed to execute query "null". Check the query syntax for correctness. See nested exception for details.
        at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:872)
        at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:794)
        at org.apache.openjpa.kernel.DelegatingQuery.execute(DelegatingQuery.java:542)
        at org.apache.openjpa.persistence.QueryImpl.execute(QueryImpl.java:286)
        at org.apache.openjpa.persistence.QueryImpl.getResultList(QueryImpl.java:302)
        at uk.gov.scotland.afrc.customer.service.entity.impl.LocationManagerImpl.locationAdvancedSearchCount(LocationManagerImpl.java:185)
        at entity.impl.LocationManagerImpl.locationAdvancedSearch(LocationManagerImpl.java:323)
        at Proxye7f1a3be_740d_413a_8d0e_b1416a65cdbc.locationAdvancedSearch(Unknown Source)
        at service.task.impl.LocationMaintenanceServiceImpl.locationAdvancedSearch(LocationMaintenanceServiceImpl.java:798)
        at Proxy3d258e3e_eb6f_4ca5_929d_23a654ac83a7.locationAdvancedSearch(Unknown Source)
        at Proxya98e61ee_2050_46bd_bcb5_7a21e398d8fd.locationAdvancedSearch(Unknown Source)
        at webservice.locationmaintenanceservice.impl.LocationMaintenanceServicePortImpl.locationAdvancedSearch(LocationMaintenanceServicePortImpl.java:295)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:180)
        at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:96)
        at org.apache.cxf.jaxws.AbstractJAXWSMethodInvoker.invoke(AbstractJAXWSMethodInvoker.java:178)
        at org.apache.cxf.jaxws.JAXWSMethodInvoker.invoke(JAXWSMethodInvoker.java:64)
        at org.apache.cxf.service.invoker.AbstractInvoker.invoke(AbstractInvoker.java:75)
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor$1.run(ServiceInvokerInterceptor.java:58)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:441)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
        at java.util.concurrent.FutureTask.run(FutureTask.java:138)
        at org.apache.cxf.workqueue.SynchronousExecutor.execute(SynchronousExecutor.java:37)
        at org.apache.cxf.interceptor.ServiceInvokerInterceptor.handleMessage(ServiceInvokerInterceptor.java:107)
        at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:262)
        at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
        at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:236)
        at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:214)
        at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:194)
        at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:130)
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:221)
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:141)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:713)
        at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:197)
        at sun.reflect.GeneratedMethodAccessor207.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.ops4j.pax.web.service.internal.HttpServiceStarted$1.invoke(HttpServiceStarted.java:182)
        at org.ops4j.pax.web.service.internal.$Proxy6.service(Unknown Source)
        at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:652)
        at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:447)
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:70)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
        at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:534)
        at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:227)
        at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1038)
        at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:117)
        at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:374)
        at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:189)
        at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:972)
        at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
        at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:75)
        at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
        at org.eclipse.jetty.server.Server.handle(Server.java:363)
        at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:483)
        at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:931)
        at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:992)
        at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:856)
        at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240)
        at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:627)
        at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:51)
        at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
        at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
        at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.RuntimeException: Can not find constructor for "class model.domain.VwLocationBrief" with argument types "[class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, class java.lang.String, long, class java.lang.String, long, class java.lang.String, boolean, class java.lang.String, class java.lang.String]" to fill d
        at org.apache.openjpa.kernel.FillStrategy$NewInstance.findConstructor(FillStrategy.java:139)
        at org.apache.openjpa.kernel.FillStrategy$NewInstance.fill(FillStrategy.java:144)
        at org.apache.openjpa.kernel.ResultShape.pack(ResultShape.java:362)
        at org.apache.openjpa.kernel.ResultShapePacker.pack(ResultShapePacker.java:64)
        at org.apache.openjpa.kernel.QueryImpl$PackingResultObjectProvider.getResultObject(QueryImpl.java:2082)
        at org.apache.openjpa.lib.rop.EagerResultList.<init>(EagerResultList.java:36)
        at org.apache.openjpa.kernel.QueryImpl.toResult(QueryImpl.java:1251)
        at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:1007)
        at org.apache.openjpa.kernel.QueryImpl.execute(QueryImpl.java:863)
        ... 67 more

我已经创建了构造函数。 现在有人可以告诉我这个运行时错误是什么吗?

【问题讨论】:

所以用确切的消息说明异常和堆栈跟踪 @NeilStockton 你好,谢谢。我对英语的理解有限。你的意思是让我把整个异常消息都放上去吗? 是的。调用“e.printStackTrace()”并将其添加到您的问题中 @NeilStockton 非常感谢。我会的。。 【参考方案1】:

您的多选正在选择与您的构造函数不匹配的字段。返回查询候选类型实例的一种更简单的方法是使用

criteriaQuery.select(root);

而不是criteriaQuery.multiselect(...)

【讨论】:

感谢您的回复。有什么方法可以选择我只需要的特定列?为了使 DISTINCT 适用于我的特定视图,我必须从根类型中选择某些列而不是整个字段。 当然有 ... 使用 multiselect() 但是你的查询的 RESULT 类必须有一个与类型匹配的构造函数 @NeilStokton 你好,尼尔。我的根类型是 VwLocationBrief 如果我使用 criteriaQuery.select(root);它从根中选择每一列,但在这种情况下,DISTINCT 不适用于我不需要的某些列。这就是我尝试使用多选的原因。你能给我一个关于这种情况的想法吗?我已经用我选择的 VwLocationBrief 类中的列创建了构造函数,但它似乎不起作用,或者我可能犯了一些错误。【参考方案2】:

我已经确认我的方法是正确的。 使用 CriteriaQuery.multiselect 时,我必须使用需要选择的字段创建构造函数。

Fuse 服务器未读取文件的类文件例外。 当我刷新我的环境时,它运行良好。

谢谢。

【讨论】:

以上是关于CriteriaQuery 多选 - 运行时构造函数错误的主要内容,如果未能解决你的问题,请参考以下文章

CriteriaQuery JPA 2 问题与结果

混合 JPQL 和 CriteriaQuery

JPA 2 CriteriaQuery,使用限制

完成选择时的jQuery多选事件

CriteriaQuery 设置可缓存

如何动态创建 CriteriaQuery