关于DataManager使用过程中出现的异常

在screen的java类中,能够使用DataManager或者使用data loader去做相关的查询,最后应该都是基于DataManager的实现吧?测试了一下,在screen的xml中定义了如下的query string

<collection id="resultsDc" class="com.vcube.serviceproject.entity.Event">
            <loader id="resultsDl">
                <query>
                    <![CDATA[
                        select e.faultReason.name as name,count(e.faultReason) as amount from serviceproject_Event e group by e.faultReason
                    ]]>
                <condition>
                    <and>
                        <c:jpql>
                            <c:where>e.device.deviceType=:deviceType</c:where>
                        </c:jpql>
                        <c:jpql>
                            <c:where>e.eventDate >=:startDate</c:where>
                        </c:jpql>
                        <c:jpql>
                            <c:where>e.eventDate <![CDATA[ <= ]]> :endDate</c:where>
                        </c:jpql>
                    </and>
                </condition>
                </query>
            </loader>
        </collection>

在java类中这样去使用:

 @Subscribe("searchAction")
    private void onSearchAction(Action.ActionPerformedEvent event) {
        if (startDate.getValue()!=null){
            resultsDl.setParameter("startDate",startDate.getValue());
        }else {
            resultsDl.removeParameter("startDate");
        }

        if (endDate.getValue()!=null){
            resultsDl.setParameter("endDate",endDate.getValue());
        }else {
            resultsDl.removeParameter("endDate");
        }

        resultsDl.setParameter("deviceType",deviceType.getValue());
        resultsDl.load();

    }

运行时出现如下异常信息:

com.haulmont.cuba.core.global.DevelopmentException: DataManager cannot execute query for single attributes
	at com.haulmont.cuba.core.app.RdbmsStore.executeQuery(RdbmsStore.java:762)
	at com.haulmont.cuba.core.app.RdbmsStore.getResultList(RdbmsStore.java:664)
	at com.haulmont.cuba.core.app.RdbmsStore.loadList(RdbmsStore.java:225)
	at com.haulmont.cuba.core.app.DataManagerBean.loadList(DataManagerBean.java:74)
	at com.haulmont.cuba.core.app.DataServiceBean.loadList(DataServiceBean.java:54)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:88)
	at com.haulmont.cuba.core.sys.ServiceInterceptor.aroundInvoke(ServiceInterceptor.java:116)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:644)
	at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:633)
	at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
	at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
	at com.sun.proxy.$Proxy197.loadList(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.haulmont.cuba.core.sys.remoting.LocalServiceInvokerImpl.invoke(LocalServiceInvokerImpl.java:94)
	at com.haulmont.cuba.web.sys.remoting.LocalServiceProxy$LocalServiceInvocationHandler.invoke(LocalServiceProxy.java:154)
	at com.sun.proxy.$Proxy35.loadList(Unknown Source)
	at com.haulmont.cuba.client.sys.DataManagerClientImpl.loadList(DataManagerClientImpl.java:57)
	at com.haulmont.cuba.gui.model.impl.CollectionLoaderImpl.load(CollectionLoaderImpl.java:88)
	at com.vcube.serviceproject.web.charts.Faultreasonpie.onSearchAction(Faultreasonpie.java:97)
	at com.haulmont.bali.events.EventHub.publish(EventHub.java:170)
	at com.haulmont.cuba.gui.components.actions.BaseAction.actionPerform(BaseAction.java:221)
	at com.haulmont.cuba.web.gui.components.WebButton.buttonClicked(WebButton.java:64)
	at com.haulmont.cuba.web.widgets.CubaButton.fireClick(CubaButton.java:76)
	at com.vaadin.ui.Button$1.click(Button.java:57)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:153)
	at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:115)
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:431)
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:396)
	at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:260)
	at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:82)
	at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
	at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1577)
	at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:425)
	at com.haulmont.cuba.web.sys.CubaApplicationServlet.serviceAppRequest(CubaApplicationServlet.java:329)
	at com.haulmont.cuba.web.sys.CubaApplicationServlet.service(CubaApplicationServlet.java:215)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:107)
	at org.springframework.web.filter.CompositeFilter.doFilter(CompositeFilter.java:73)
	at com.haulmont.cuba.web.sys.CubaHttpFilter.doFilter(CubaHttpFilter.java:108)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:200)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:836)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1747)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)

DataManager不能够支持这种查询?

把你的完整的JPQL 语句发一下

用java string表示就是这种的

select e.faultReason.name as name,count(e.faultReason) as amount from serviceproject_Event where e.device.deviceType = :deviceType and e.eventDate = :startDate and e.eventDate <= :endDate e group by e.faultReason 

screen xml文件中的语句在这里显示不全,我放到附件中了Faultreasonpie.java (3.3 KB) faultReasonPie.xml (2.9 KB)

发大段代码或者日志的时候,注意要格式化你的代码,方便阅读。

DataManager 不支持聚合查询或单值查询,只能用来加载实体。对于这种查询,目前的做法是提供一个服务,在服务实现中使用 EntityManager 来加载数据。 然后自定义DataLoader的数据获取方法 ,在这个方法中调用获取数据的服务。自定义数据获取方法的文档在这里:https://doc.cuba-platform.cn/manual-7.0-chs/gui_data_loaders.html

你先尝试这样实现。

1赞

另外也可以试试数据库视图的方式,就是创建一个用于数据汇总的数据库视图,这个视图对应一个实体。然后从这个“视图实体”查询。 这种方式可能更直接一点。

1赞

如果不想取实体,只取部分字段,或者取sum ,max等sql结果,可以用 KeyValueEntity, 也支持datamanager。

https://doc.cuba-platform.com/manual-latest/dataManager.html

请问用实体区对应视图的话,应该怎么操作,创建persistent实体还是not persistent实体对应。能否给个例子

昨晚测试了一下,直接创建一个继承StandardEntity的实体类可以直接映射到数据库的视图上,至少查询列表可以展示。数据库视图需要将相关的id,create_ts之类的列创建。

1赞