使用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";
答案 0 :(得分:1)
您使用哪种继承策略?
如果它包含一个鉴别符列,则可以始终将其映射为常规列,就像其他任何属性一样。然后它将可用于查询。
示例:
@Entity
@Inheritance(strategy = JOINED)
@DiscriminatorColumn(name = "dtype")
public abstract class Bill {
@Column(name = "dtype", insertable=false, updatable=false)
private String type;
...
}
话虽这么说,我会认真考虑扁平化继承结构。听起来好像Bill
可以变成BilledBill
或CancelledBill
,所以Bill
拥有status
属性也许就足够了?