在 JPQL 中按 Object 的 Class 分组

Group by the Class of Object in JPQL

带有 JPA 和 JSF 的 JavaEE 应用程序有一个 Bill 实体,它是 class 到 BilledBill、CanceledBill 和 RefundBill 的子实体。 我使用 EclipseLink 作为持久性提供者。 我想编写一个查询,按 class 的类型对总和进行分组。 我累了,但失败了。 这可能吗?

相关构造函数如下

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";

您使用哪种继承策略?

如果它涉及鉴别器列,您始终可以将其映射为常规列,就像任何其他列一样 属性。然后它将可用于查询。

示例:

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

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

话虽如此,我会认真考虑扁平化继承结构。听起来好像 Bill 可以变成 BilledBillCancelledBill,所以也许 Billstatus 属性 就足够了] 反而?