0

我尝试通过在 RapidClipse4 中使用本机查询来生成交叉表。
数据已收集并在控制台上输出仍然可用,但分配给 xDevTable 不起作用。

我创建了以下代码:

private void button_buttonClick(final Button.ClickEvent event) {

        try {
            final String selJahr = this.cmbJahr.getSelectedItem().getBean().getJahr();
            final StringBuilder sb = new StringBuilder();
            sb.append("select FLOOR(RAND() * 999999) as rid, l1_Id as `l1id`, l1_GroupName as `l1GroupName`, l2_Id as `l2id`, l2_GroupName as `l2GroupName`, ");
            sb.append("sum( case when (monat = 1) then dbetrag end ) AS `Jan`, ");
            sb.append("sum( case when monat = 2 then dbetrag end ) AS `Feb`, ");
            sb.append("sum( case when monat = 3 then dbetrag end ) AS `Mrz`, ");
            sb.append("sum( case when monat = 4 then dbetrag end ) AS `Apr`, ");
            sb.append("sum( case when monat = 5 then dbetrag end ) AS `Mai`, ");
            sb.append("sum( case when monat = 6 then dbetrag end ) AS `Jun`, ");
            sb.append("sum( case when monat = 7 then dbetrag end ) AS `Jul`, ");
            sb.append("sum( case when monat = 8 then dbetrag end ) AS `Aug`, ");
            sb.append("sum( case when monat = 9 then dbetrag end ) AS `Sep`, ");
            sb.append("sum( case when monat = 10 then dbetrag end ) AS `Okt`, ");
            sb.append("sum( case when monat = 11 then dbetrag end ) AS `Nov`, ");
            sb.append("sum( case when monat = 12 then dbetrag end ) AS `Dez`, ");
            sb.append("sum(dbetrag) as `Sum` ");
            sb.append("from V_HibCrosUmsGiroOpaOmaReport ");
            sb.append("where  jahr='2019' and konto_Id in ('7,8') ");
            sb.append("group by l1_Id, l1_GroupName, l2_Id, l2_GroupName; ");

            final String sqlq = sb.toString();
            final EntityManagerFactory emf = Persistence.createEntityManagerFactory("RC_07");
            final EntityManager em = emf.createEntityManager();

            System.out.print( "SQL String: " + sqlq + "/n");

            final Query q = em.createNativeQuery(sqlq);
            // will be later used:
            //q.setParameter(1, selJahr);
            //q.setParameter(2, "7,8");
            // final List<VUmsCrosstbl> VUmsCrosstbl = q.getResultList();


            final XdevBeanItemContainer<VUmsCrosstbl> myContainer = new XdevBeanItemContainer<>(VUmsCrosstbl.class);
            myContainer.addAll(q.getResultList());

            this.tblUmsCross.setContainerDataSource(myContainer);
            this.tblUmsCross.setVisibleColumns(VUmsCrosstbl_.rid.getName(),VUmsCrosstbl_.l1Id.getName(),VUmsCrosstbl_.l1GroupName.getName(),VUmsCrosstbl_.l2Id.getName(),VUmsCrosstbl_.l2GroupName.getName(),VUmsCrosstbl_.jan.getName(),VUmsCrosstbl_.feb.getName(),VUmsCrosstbl_.mrz.getName(),VUmsCrosstbl_.apr.getName(),VUmsCrosstbl_.mai.getName(),VUmsCrosstbl_.jun.getName(),VUmsCrosstbl_.jul.getName(),VUmsCrosstbl_.aug.getName(),VUmsCrosstbl_.sep.getName(),VUmsCrosstbl_.okt.getName(),VUmsCrosstbl_.nov.getName(),VUmsCrosstbl_.dez.getName(),VUmsCrosstbl_.sum.getName());
            this.tblUmsCross.setId(VUmsCrosstbl_.rid.getName());
            this.tblUmsCross.setColumnHeaders(new String[] {"rid", "l1_Id", "l1_GroupName", "l2_Id", "l2_GroupName", "Jan","Feb","Mrz","Apr","Mai","Jun","Jul","Aug","Sep","Okt","Nov","Dez", "Sum"});
            this.tblUmsCross.addItems(q.getResultList());
            this.tblUmsCross.getBeanContainerDataSource().refresh();


            final List<Object[]> VUmsCrosstbl = q.getResultList();
            for (final Object[] a : VUmsCrosstbl) {
                System.out.println("Ds: " + a[0] + " " + a[1] + " " + a[2] + " " + a[3] + " " + a[4] + " " + a[5] + " " + a[6] + " " + a[7] + " " + a[8] + " " + a[9] + " " + a[10] + " " + a[11] + " " + a[12] + " " + a[13] + " " + a[14] + " " + a[15]);
            }

            em.close();

        } catch (final Exception e) {
            e.printStackTrace();
            Notification.show("Do isch was falsch bei button_buttonClick  ", e.getMessage(),
                    Notification.Type.ERROR_MESSAGE);
        }

    }

该过程在没有任何错误
的情况下运行代码“ System.out.println("Ds: ..." ”末尾的行仅用于检查容器是否有数据。这些行在控制台上打印出完全预期的输出和所有预期的数据

在应用程序中,列(标题)仍然被创建并且可见,但没有包含数据的行。单元格是空的。

我的错误在哪里/我做错了什么?任何想法/帮助将不胜感激。

我想,你也应该知道:

我在查询中使用的表名与实体名称“VUmsCrosstbl”不同

为什么?

我使用的实体名称“VUmsCrosstbl”是一个 db-view,其中包含我作为查询结果集获得的字段。

有了这个技巧,我可以使用 rapidclipse 中的实体构建器,它会自动生成实体和 dao。

对于我在查询中使用的 View V_HibCrosUmsGiroOpaOmaReport,月份名称 Jan-Dez 仍然不在我第一次使用并产生其他错误的实体或 dao 中。


提前谢谢你 rgds OpaHeinz

4

1 回答 1

0

我自己找到了解决方案:

1)我仍然不确定为什么上面的代码不起作用,我只能假设,分配过程中有问题:


    myContainer.addAll(q.getResultList()); 

为了让它工作,我更改了 DAU 并放置了以下代码:


            public List<VUmsCrosstbl> getAllCrossDataforYearKonto2 ()
    {  //final String pjahr, final String pKontoIDs

        final StringBuilder sb = new StringBuilder();
                sb.append("select FLOOR(RAND() * 999999) as rid, l1_Id, L1_GroupName, l2_Id, L2_GroupName, ");
                sb.append("sum( case when (monat = 1) then dbetrag end ) AS `Jan`, ");
                sb.append("sum( case when monat = 2 then dbetrag end ) AS `Feb`, ");
                sb.append("sum( case when monat = 3 then dbetrag end ) AS `Mrz`, ");
                sb.append("sum( case when monat = 4 then dbetrag end ) AS `Apr`, ");
                sb.append("sum( case when monat = 5 then dbetrag end ) AS `Mai`, ");
                sb.append("sum( case when monat = 6 then dbetrag end ) AS `Jun`, ");
                sb.append("sum( case when monat = 7 then dbetrag end ) AS `Jul`, ");
                sb.append("sum( case when monat = 8 then dbetrag end ) AS `Aug`, ");
                sb.append("sum( case when monat = 9 then dbetrag end ) AS `Sep`, ");
                sb.append("sum( case when monat = 10 then dbetrag end ) AS `Okt`, ");
                sb.append("sum( case when monat = 11 then dbetrag end ) AS `Nov`, ");
                sb.append("sum( case when monat = 12 then dbetrag end ) AS `Dez`, ");
                sb.append("sum(dbetrag) as `Sum` ");
                sb.append("from V_HibCrosUmsGiroOpaOmaReport ");
                sb.append("where  jahr= ? and konto_Id in ( ? ) ");
                sb.append("group by l1_Id, L1_GroupName, l2_Id, L2_GroupName; ");
                final String sqlq = sb.toString();


        final Query nativeQuery = em().createNativeQuery(sqlq, VUmsCrosstbl.class);
        nativeQuery.setParameter(1, "2019");
        nativeQuery.setParameter(2, "7,8");
        final List<VUmsCrosstbl> resultList = nativeQuery.getResultList();

        return resultList;
    }

然后我在按钮的点击事件中使用了这个函数,将结果分配到 XDevBeanitemContainer 和表中:


        private void button_buttonClick(final Button.ClickEvent event) {

            final XdevBeanItemContainer<VUmsCrosstbl> myContainer = new XdevBeanItemContainer<>(VUmsCrosstbl.class);
            myContainer.addAll(new VUmsCrosstblDAO().getAllCrossDataforYearKonto2());

            this.tblUmsCross.setContainerDataSource(myContainer);
            this.tblUmsCross.setId(VUmsCrosstbl_.rid.getName());
            this.tblUmsCross.setVisibleColumns(VUmsCrosstbl_.rid.getName(),VUmsCrosstbl_.l1Id.getName(),VUmsCrosstbl_.l1GroupName.getName(),VUmsCrosstbl_.l2Id.getName(),VUmsCrosstbl_.l2GroupName.getName(),VUmsCrosstbl_.jan.getName(),VUmsCrosstbl_.feb.getName(),VUmsCrosstbl_.mrz.getName(),VUmsCrosstbl_.apr.getName(),VUmsCrosstbl_.mai.getName(),VUmsCrosstbl_.jun.getName(),VUmsCrosstbl_.jul.getName(),VUmsCrosstbl_.aug.getName(),VUmsCrosstbl_.sep.getName(),VUmsCrosstbl_.okt.getName(),VUmsCrosstbl_.nov.getName(),VUmsCrosstbl_.dez.getName(),VUmsCrosstbl_.sum.getName());
            this.tblUmsCross.setColumnHeaders(new String[] {"rid", "l1_Id", "l1_GroupName", "l2_Id", "l2_GroupName", "Jan","Feb","Mrz","Apr","Mai","Jun","Jul","Aug","Sep","Okt","Nov","Dez", "Sum"});


        }

在此之后,数据在表中可见。
我很高兴解决了这个问题。
rgds
奥帕海因茨

于 2019-08-04T08:37:44.177 回答