CriteriaQuery multiselect - 运行时构造函数错误

时间:2015-11-06 17:57:51

标签: java jpa runtime criteriaquery

我使用CriteriaQuery创建具有特定类类型的TypedQuery。 我只需要类类中的几列。 我通过multiselect方法选择那些列,但我正在努力使用下面的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

我已经创建了构造函数。 现在有人能告诉我,我有什么运行时错误吗?

2 个答案:

答案 0 :(得分:0)

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

criteriaQuery.select(root);

而不是criteriaQuery.multiselect(...)

答案 1 :(得分:0)

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

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

感谢。