JPQL Group By TimeStamp作为日期

时间:2014-08-26 18:14:44

标签: jpa jpql

在JPA应用程序中,Bill实体有一个名为createAt的属性,带有TimeStamp注释。

我想按创建时仅按日期对比尔进行分组,而不是按TimeStamp进行分组。

在查询中,虽然我将TemporalType作为日期传递,但仍将账单分组为时间。

如何使用createdAt属性按日期对Bills进行分组。

我将JPA与EclipseLink 2.5一起使用。

比尔实体

@Entity
public class Bill implements Serializable {
    ....
    ....

    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    Date createdAt;

    ....
    ....
}

这是用于列出账单的方法。

public String listProfitBillsDailySummery() {
    System.out.println("list profit bills");
    String jpql = "SELECT new com.divudi.data.DailySummeryRow(b.createdAt, b.freeValue, b.netTotal, b.discount) "
            + " FROM Bill b "
            + " WHERE (type(b)=:bc1 or type(b)=:bc2 or type(b)=:bc3 ) "
            + " and b.retired=false "
            + " and (b.billType=:bt1 or b.billType=:bt2 or b.billType=:bt3) "
            + " and b.createdAt between :fromDate and :toDate ";

    Map temMap = new HashMap();

    if (department != null) {
        jpql += " and b.department=:d ";
        temMap.put("d", department);
    }

    jpql += " group by b.createdAt "
            + "order by b.deptId  ";

    temMap.put("bc1", BilledBill.class);
    temMap.put("bc2", RefundBill.class);
    temMap.put("bc3", CancelledBill.class);

    temMap.put("bt1", BillType.PharmacyPurchaseBill);
    temMap.put("bt2", BillType.PharmacyGrnBill);
    temMap.put("bt3", BillType.PharmacySale);

    temMap.put("fromDate", getFromDate());
    temMap.put("toDate", getToDate());

    List<Object[]> dsso = getBillFacade().findAggregates(jpql, temMap, TemporalType.DATE);
    profitTotal = 0.0;
    discountTotal = 0.0;
    freeTotal =0.0;
    dailySummeryRows = new ArrayList<>();
    for (Object b : dsso) {
        DailySummeryRow dsr = (DailySummeryRow) b;
        profitTotal += dsr.getProfit();
        discountTotal += dsr.getDiscounts();
        freeTotal += dsr.getFreeAmounts();
        dailySummeryRows.add(dsr);
    }
    return "pharmacy_report_gross_profit_by_bills_ds";
}

这是会话Bean中的方法

public List<Object[]> findAggregates(String temSQL, Map<String, Object> parameters, TemporalType tt) {
        TypedQuery<Object[]> qry = getEntityManager().createQuery(temSQL, Object[].class);
        Set s = parameters.entrySet();
        Iterator it = s.iterator();
        while (it.hasNext()) {
            Map.Entry m = (Map.Entry) it.next();
            Object pVal =  m.getValue();
            String pPara = (String) m.getKey();
            if(pVal instanceof Date){
                Date pDate = (Date) pVal;
                qry.setParameter(pPara, pDate, TemporalType.DATE);
            }else{
                qry.setParameter(pPara, pVal);
            }
        }
        try {
            return qry.getResultList();
        } catch (Exception e) {
            System.out.println("e = " + e);
            return null;
        }
    }

1 个答案:

答案 0 :(得分:0)

我使用FUNC('Date',b.createdAt)按日期分组。

public String listProfitBillsDailySummery() {
    System.out.println("list profit bills");
    String jpql;
     jpql = "SELECT new com.divudi.data.DailySummeryRow(FUNC('DATE',b.createdAt), b.freeValue, b.netTotal, b.discount) "
            + " FROM Bill b "
            + " WHERE (type(b)=:bc1 or type(b)=:bc2 or type(b)=:bc3 ) "
            + " and b.retired=false "
            + " and (b.billType=:bt1 or b.billType=:bt2 or b.billType=:bt3) "
            + " and FUNC('DATE',b.createdAt) between :fromDate and :toDate ";

    Map temMap = new HashMap();

    if (department != null) {
        jpql += " and b.department=:d ";
        temMap.put("d", department);
    }

    jpql += " group by FUNC('DATE',b.createdAt) "
            + "order by FUNC('DATE',b.createdAt)  ";

    temMap.put("bc1", BilledBill.class);
    temMap.put("bc2", RefundBill.class);
    temMap.put("bc3", CancelledBill.class);

    temMap.put("bt1", BillType.PharmacyPurchaseBill);
    temMap.put("bt2", BillType.PharmacyGrnBill);
    temMap.put("bt3", BillType.PharmacySale);

    temMap.put("fromDate", getFromDate());
    temMap.put("toDate", getToDate());

    List<Object[]> dsso = getBillFacade().findAggregates(jpql, temMap, TemporalType.DATE);
    profitTotal = 0.0;
    discountTotal = 0.0;
    freeTotal =0.0;
    dailySummeryRows = new ArrayList<>();
    if(dsso==null){
        dsso = new ArrayList<>();
        System.out.println("new list as null");
    }
    for (Object b : dsso) {
        DailySummeryRow dsr = (DailySummeryRow) b;
        profitTotal += dsr.getProfit();
        discountTotal += dsr.getDiscounts();
        freeTotal += dsr.getFreeAmounts();
        dailySummeryRows.add(dsr);
    }
    return "pharmacy_report_gross_profit_by_bills_ds";
}