【发布时间】:2018-12-20 23:45:18
【问题描述】:
带有 JPA 和 JSF 的 JavaEE 应用程序有一个账单实体,它是 BilledBill、CancelledBill 和 RefundBill 的子类。 我使用 EclipseLink 作为持久性提供者。 我想编写一个查询,按类的类型对总数进行分组。 我累了,但失败了。 这可能吗?
相关构造函数如下。
public BillSummery(PaymentMethod paymentMethod, Class billClass, Double total, Double discount, Double netTotal, Double tax, Long count, BillType billType) {
this.paymentMethod = paymentMethod;
this.total = total;
this.discount = discount;
this.netTotal = netTotal;
this.tax = tax;
this.count = count;
this.billType = billType;
this.BillClass = billClass;
}
JPQL如下
String j = "select new com.divudi.data.BillSummery(b.paymentMethod, type(b), sum(b.total), sum(b.discount), sum(b.netTotal), sum(b.vat), count(b), b.billType) "
+ " from Bill b "
+ " where b.retired=false "
+ " and b.billTime between :fd and :td "
+ " group by b.paymentMethod, type(b), b.billType";
【问题讨论】:
-
你是什么意思你失败了 - 你得到一个错误还是出了什么问题?简化您的查询,只需按类型(b)对总数和分组求和,然后从那里开始计算。 “选择 sum(b.total), type(b) as bill_type from Bill b group by bill_type”。构建 JPQL 查询后,查看返回类型并确保它们适合您的构造函数
标签: jpa eclipselink jpql