按JPQL中的对象类别分组

时间:2018-07-12 16:46:44

标签: jpa eclipselink jpql

使用JPA和JSF的JavaEE应用程序具有一个Bill实体,该实体被分类为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";

1 个答案:

答案 0 :(得分:1)

您使用哪种继承策略?

如果它包含一个鉴别符列,则可以始终将其映射为常规列,就像其他任何属性一样。然后它将可用于查询。

示例:

@Entity
@Inheritance(strategy = JOINED)
@DiscriminatorColumn(name = "dtype")
public abstract class Bill {

    @Column(name = "dtype", insertable=false, updatable=false)
    private String type;
    ...
}

话虽这么说,我会认真考虑扁平化继承结构。听起来好像Bill可以变成BilledBillCancelledBill,所以Bill拥有status属性也许就足够了?