MetaClass CollectionLoaderImpl Cast Exception

Hello everyone,
I’m trying to feed the chart data through Collectionloader
but I get the error below,

The scenario is :

@Service(DataStatService.NAME)
public class DataStatServiceBean implements DataStatService {

    public static final String NAME = "dashboarddemo_DataStatServiceBean";

    public List<FactTableStats> loadfactTable(LoadContext<FactTableStats> lc) {
    List<FactTableStats> qr = em.getDelegate().createNativeQuery(" some select of FactTableStat fields ").getResultList;

 @MetaClass(name = "FactTableStats")
 public class FactTableStats extends BaseGenericIdEntity {

     @MetaProperty
     protected String desArticolo;

     @MetaProperty
     protected String desCateg1;  ...

The fragment:

 <fragment xmlns="http://schemas.haulmont.com/cuba/screen/fragment.xsd"
           xmlns:chart="http://schemas.haulmont.com/charts/charts.xsd">
    <data>
        <collection id="dateValueDc" class="com.haulmont.demo.dashboard.entity.FactTableStats" view="_local">
             <loader  id="dateValueDl"/>
         </collection>
     </data>

The Loader:

     @Subscribe(target = Target.PARENT_CONTROLLER)
     private void onBeforeShowHost(Screen.BeforeShowEvent event) {
         dateValueDl.load();
     }
 
     @Install(to = "dateValueDl", target = Target.DATA_LOADER)
     protected List<FactTableStats> dateValueDlLoadDelegate(LoadContext<FactTableStats> loadContext) {
 
         return  dataStatService.loadfactTable(loadContext);
 
     }

In the List<FactTableStats> returned there are some elements …

But an error is caused by CollectionLoaderImpl

      if (dataContext != null) {
             List<E> mergedList = new ArrayList<>(list.size());
  /* CollectionLoaderImpl.java:95 */           for (E entity : list) {  
                 mergedList.add(dataContext.merge(entity));
             }
             container.setItems(mergedList);
         } else {
             container.setItems(list);
         }

Error on cuba platform 7.0.5 :

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.haulmont.cuba.core.entity.Entity
	at com.haulmont.cuba.gui.model.impl.CollectionLoaderImpl.load(CollectionLoaderImpl.java:95)
	at com.haulmont.demo.dashboard.web.viewstat5f.Viewstat5fFragment.onBeforeShowHost(Viewstat5fFragment.java:57)
	at com.haulmont.bali.events.EventHub.publish(EventHub.java:170)
	at com.haulmont.cuba.gui.screen.Screen.fireEvent(Screen.java:128)
	at com.haulmont.cuba.gui.screen.UiControllerUtils.fireEvent(UiControllerUtils.java:58)
	at com.haulmont.cuba.web.sys.WebScreens.show(WebScreens.java:422)
	at com.haulmont.cuba.web.sys.WebScreens.showFromNavigation(WebScreens.java:528)
	at com.haulmont.cuba.gui.config.MenuItemCommands$ScreenCommand.run(MenuItemCommands.java:213)

Thank you in advance for any suggestions.

PS:

I thing is an rpc / serialization<->deserialization problem …

Hi!

Probably, the exception is occurred because of the result of your query is a vector of object arrays. But you can try a more simple way to feed the chart. See KeyValueContainer and following samples:

Note that charts can be fed not only with dataContainer. You can set ListDataProvider to the chart using Chart#setDataProvider().

See examples:

Hi Roman, thank you for your reply and congratulations for the excellent work you do.
In this case I believe there is a problem in transferring the entity class vector from the core service to the ScreenFragment, I found that even just trying to print the contents of the List causes error … the debugger allows me to view the contents of the List but as soon as I try to use it I get conversion exceptions … I thing vaadin is involved in this problem.

If I use a database @Table based class instead of a @MetaClass then everything works …

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.haulmont.demo.dashboard.entity.FactTableStats
	at java.util.Vector.forEach(Unknown Source)
	at com.haulmont.demo.dashboard.web.viewstat5f.Viewstat5fFragment.onBeforeShowHost(Viewstat5fFragment.java:45)
	at com.haulmont.bali.events.EventHub.publish(EventHub.java:170)
	at com.haulmont.cuba.gui.screen.Screen.fireEvent(Screen.java:128)
	at com.haulmont.cuba.gui.screen.UiControllerUtils.fireEvent(UiControllerUtils.java:58)
	at com.haulmont.cuba.web.sys.WebScreens.show(WebScreens.java:422)
	at com.haulmont.cuba.web.sys.WebScreens.showFromNavigation(WebScreens.java:528)
	at com.haulmont.cuba.gui.config.MenuItemCommands$ScreenCommand.run(MenuItemCommands.java:213)
	at com.haulmont.cuba.web.sys.MenuBuilder$MenuCommandExecutor.accept(MenuBuilder.java:256)
	at com.haulmont.cuba.web.sys.MenuBuilder$MenuCommandExecutor.accept(MenuBuilder.java:241)
	at com.haulmont.cuba.web.gui.components.mainwindow.WebAppMenu$MenuItemImpl.menuSelected(WebAppMenu.java:435)
	at com.vaadin.ui.MenuBar.changeVariables(MenuBar.java:225)
	at com.vaadin.server.communication.ServerRpcHandler.changeVariables(ServerRpcHandler.java:611)
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:457)
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:400)
	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:199)
	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:668)
	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:834)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Unknown Source)

When you use setNativeQuery() and select individual columns you will get rows as Object[] (tuple) not as certain entity. That is why an exception occurs it can’t cast this array to an entity. In this case, you should manually create entities from the given result. See example below:

DemoTask is meta class with two fields

@Inject
private Persistence persistence;
@Inject
private Metadata metadata;

@Override
public List<DemoTask> loadDemoTask(LoadContext<DemoTask> lc) {
    List<DemoTask> result = new ArrayList<>();
    try (Transaction tx = persistence.createTransaction()) {
        EntityManager em = persistence.getEntityManager();
        List list = em.createNativeQuery("select NAME, ESTIMATION from DEMOPROJECT_TASK").getResultList();
        for (Iterator it = list.iterator(); it.hasNext(); ) {
            Object[] row = (Object[]) it.next();
			
            DemoTask item = metadata.create(DemoTask.class);
            item.setName((String) row[0]);
            item.setEndDate((Date) row[1]);
            result.add(item);
        }
        tx.commit();
    }
    return result;
}

Documentation about nativeQuery you can find here: documentation.

1 Like

MayBe this is my case ?

https://www.cuba-platform.com/discuss/t/how-to-use-non-entity-result-class-for-native-query-in-cuba/3712

I need to create a service that builds dinamically some query e put the results in a class that is a meta class …

Like in this example → Creating an Entity - CUBA Platform. Displaying Charts And Maps

This can be the answer ? → Screen Controller - CUBA Platform. Displaying Charts And Maps

My actual code is:

try (Transaction tx = persistence.createTransaction("STATISTICHE")) {
    EntityManager em = persistence.getEntityManager( "STATISTICHE");

    TypedQuery<FactTableStats> query = em.createNativeQuery(
            " SELECT ditta as dimNegozio, null as desNegozio, null as dimTempo, null as dimTempoFascia, null as dimArticolo, null as desArticolo, cod_rep as dimRep, null desRep," +
                    "         null as dimCateg1, null as desCateg1, null as dimCateg2, null as desCateg2, null as dimCateg3, null as desCateg3, null as dimCateg4, null as desCateg4," +
                    "         sum(val_rep) as misValore, null as misVolume, null as misNumero" +
                    "  FROM Stat5fasce WHERE ditta " + dimNegozio + " GROUP BY ditta, cod_rep ",FactTableStats.class);

    List<FactTableStats> list = query.getResultList();


    return list;

}

I have a problem with using @MetaClass with the ORM.

@MetaClass(name = "FactTableStats")
public class FactTableStats extends BaseGenericIdEntity {

    @MetaProperty
    protected String desArticolo;

    @MetaProperty
    protected String desCateg1;

    @MetaProperty
    protected String desCateg2;

    @MetaProperty
    protected String desCateg3;

… it happens that

com.haulmont.cuba.core.global.RemoteException:
---
javax.persistence.PersistenceException: Exception [EclipseLink-6007] (Eclipse Persistence Services - 2.7.3.2-cuba): org.eclipse.persistence.exceptions.QueryException
Exception Description: Missing descriptor for [class com.haulmont.demo.dashboard.entity.FactTableStats].
Query: ReadAllQuery(referenceClass=FactTableStats sql=" SELECT ditta as dimNegozio, null as desNegozio, null as dimTempo, null as dimTempoFascia, null as dimArticolo, null as desArticolo, cod_rep as dimRep, null desRep,         null as dimCateg1, null as desCateg1, null as dimCateg2, null as desCateg2, null as dimCateg3, null as desCateg3, null as dimCateg4, null as desCateg4,         sum(val_rep) as misValore, null as misVolume, null as misNumero  FROM Stat5fasce WHERE ditta  LIKE '%'  GROUP BY ditta, cod_rep ")
---
org.eclipse.persistence.exceptions.QueryException: 
Exception Description: Missing descriptor for [class com.haulmont.demo.dashboard.entity.FactTableStats].
Query: ReadAllQuery(referenceClass=FactTableStats sql=" SELECT ditta as dimNegozio, null as desNegozio, null as dimTempo, null as dimTempoFascia, null as dimArticolo, null as desArticolo, cod_rep as dimRep, null desRep,         null as dimCateg1, null as desCateg1, null as dimCateg2, null as desCateg2, null as dimCateg3, null as desCateg3, null as dimCateg4, null as desCateg4,         sum(val_rep) as misValore, null as misVolume, null as misNumero  FROM Stat5fasce WHERE ditta  LIKE '%'  GROUP BY ditta, cod_rep ")
	at com.haulmont.cuba.core.sys.ServiceInterceptor.aroundInvoke(ServiceInterceptor.java:129)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	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.$Proxy273.loadfactTable(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	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.$Proxy66.loadfactTable(Unknown Source)
	at com.haulmont.demo.dashboard.web.viewstat5f.Viewstat5fFragment.dateValueDlLoadDelegate(Viewstat5fFragment.java:56)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at com.haulmont.cuba.gui.sys.UiControllerDependencyInjector$InstalledFunction.apply(UiControllerDependencyInjector.java:755)
	at com.haulmont.cuba.gui.model.impl.CollectionLoaderImpl.load(CollectionLoaderImpl.java:90)
	at com.haulmont.demo.dashboard.web.viewstat5f.Viewstat5fFragment.onBeforeShowHost(Viewstat5fFragment.java:50)
	at com.haulmont.bali.events.EventHub.publish(EventHub.java:170)
	at com.haulmont.cuba.gui.screen.Screen.fireEvent(Screen.java:128)
	at com.haulmont.cuba.gui.screen.UiControllerUtils.fireEvent(UiControllerUtils.java:58)
	at com.haulmont.cuba.web.sys.WebScreens.show(WebScreens.java:422)
	at com.haulmont.cuba.web.sys.WebScreens.showFromNavigation(WebScreens.java:528)
	at com.haulmont.cuba.gui.config.MenuItemCommands$ScreenCommand.run(MenuItemCommands.java:213)
	at com.haulmont.cuba.web.sys.MenuBuilder$MenuCommandExecutor.accept(MenuBuilder.java:256)
	at com.haulmont.cuba.web.sys.MenuBuilder$MenuCommandExecutor.accept(MenuBuilder.java:241)
	at com.haulmont.cuba.web.gui.components.mainwindow.WebAppMenu$MenuItemImpl.menuSelected(WebAppMenu.java:435)
	at com.vaadin.ui.MenuBar.changeVariables(MenuBar.java:225)
	at com.vaadin.server.communication.ServerRpcHandler.changeVariables(ServerRpcHandler.java:611)
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:457)
	at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:400)
	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:199)
	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:668)
	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:834)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1417)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Unknown Source)

At the end I created a table like my class in the database so that the ORM was able to map the properties and the service now returns correctly the vector of classes … also the chart is filled correctley … thank you cuba … so much !

The new class from the database …

@Table(name = "FactTable")
@Entity(name = "dashboarddemo_FactTable")
public class FactTable extends BaseGenericIdEntity {

    @Id
    @Column(name =  "id")
    protected Long id;


    @Column(name = "desArticolo")
    protected String desArticolo;
    @Column(name = "desCateg1", length = 35

The fragment …

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<fragment xmlns="http://schemas.haulmont.com/cuba/screen/fragment.xsd"
          xmlns:chart="http://schemas.haulmont.com/charts/charts.xsd">

    <data>
        <collection id="dateValueDc" class="com.haulmont.demo.dashboard.entity.FactTable" >
            <loader  id="dateValueDl"/>
        </collection>
    </data>

    <layout>
        <chart:serialChart id="lineChart"
                           autoMarginOffset="20"

                           categoryField="dimRep"

                           dataContainer="dateValueDc"
                           marginRight="40"

The loader …

   @Inject
    private CollectionLoader<FactTable> dateValueDl;

    @Inject
    private CollectionContainer<FactTable> dateValueDc;

    @Subscribe(target = Target.PARENT_CONTROLLER)
    private void onBeforeShowHost(Screen.BeforeShowEvent event) {
        dateValueDl.load();
    }

    @Install(to = "dateValueDl", target = Target.DATA_LOADER)
    protected List<FactTable> dateValueDlLoadDelegate(LoadContext<FactTable> loadContext) {

        List<FactTable> ls=  dataStatService.loadfactTable(loadContext, new ArrayList<>(Arrays.asList(String.valueOf('7'))) ,
                null,null,null,null,null,null,null,null,null);

        System.out.println(ls.size());

        return ls;
    }