mysql - JPQL 未对空值进行分组

标签 mysql jpa group-by eclipselink jpql

我有一个 JSF 应用程序,它使用 JPA 来管理数据库事务。

在我的数据结构中有账单[Bill]。一张账单可以有多个账单项目[BillItem]。账单项目与项目有关系。票据可能有一个机构和一个收集中心。但在某些法案中两者都可能无效。我想按项目、机构和收集中心对账单项目进行分组并获取计数。以下 jpql 仅列出了拥有机构和收集中心的机构。这意味着它不会对空值进行分组。

我使用 EclipseLink 2.4 和 MySQL。

    String jpql;
    Map m = new HashMap();
    jpql = "Select new com.divudi.data.dataStructure.ItemInstitutionCollectingCentreCountRow(bi.item, bi.bill.institution, bi.bill.collectingCentre, count(bi)) "
            + " from BillItem bi "
            + " where bi.bill.createdAt between :fd and :td "
            + " and type(bi.item) =:ixbt "
            + " and bi.retired=false "
            + " and bi.bill.retired=false "
            + " and bi.bill.cancelled=false "
            + " and bi.retired=false ";
    jpql = jpql + " group by bi.item, bi.bill.institution, bi.bill.collectingCentre";
    jpql = jpql + " order by bi.bill.institution.name, bi.bill.collectingCentre.name, bi.item.name ";
    m.put("fd", fromDate);
    m.put("td", toDate);
    m.put("ixbt", Investigation.class);
    insInvestigationCountRows = (List<ItemInstitutionCollectingCentreCountRow>) (Object) billFacade.findAggregates(jpql, m, TemporalType.DATE);
    System.out.println("sql = " + jpql);
    System.out.println("m = " + m);
    System.out.println("insInvestigationCountRows.size() = " + insInvestigationCountRows.size());

最佳答案

在关系之间使用点意味着内部联接将过滤掉空值。如果要在关系中允许空值,则必须显式使用外连接:

"Select count(bi) from BillItem bi JOIN bi.item item LEFT JOIN bi.bill bill LEFT JOIN bill.institution institution LEFT JOIN bill.collectingCentre collectingCentre group by item, institution, collectingCentre"

关于mysql - JPQL 未对空值进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26774608/

相关文章:

java - QueryDSL 返回最大值

mysql - 根据值对行进行分组并插入到另一个表中

r - 按组选择每个连续运行中的第一行

mysql - Django 1.7 中的 transaction.autocommit

php - 在 php 中用 ' 替换每个 '

java - 如何对eclipselink中的所有实体进行软删除(逻辑删除)

php - 如何根据 php 中的数据库字段值对日期进行分组

mysql - 是MYSQL的表关系错误吗?

MySQL ODBC 导入失败

spring - JPA 和 Hibernate Cascade DELETE OneToMany 不起作用