我采用以下方法汇总所有传递到where子句中的符合规范标准(搜索参数)的交易的总帐单金额。
这很好,直到使用了订单项搜索参数。然后,根据相关订单项的数量来复制总帐单金额。这与我将BigDecimal用作查询类有关。如果使用“交易”,结果是不同的,但是我无法将总帐单金额相加。
public BigDecimal sumBillingAmount(TransactionReportFilteredRequest request) {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<BigDecimal> query = cb.createQuery(BigDecimal.class);
Root<Transaction> root = query.from(Transaction.class);
Specification<Transaction> tranSpec = TransactionSpecification.webFilteringSearch(request.getUserLogNo(), request.getRefNo(),
request.getBillingDateFrom(), request.getBillingDateTo(), request.getTransactionDateFrom(), request.getTransactionDateTo(),
request.getFund(), request.getMinTotalChargeAmt(), request.getMaxTotalChargeAmt(), request.getStatus(), request.getCardId(),
request.getUserId(), request.getCompanyName(), request.getCenters());
query.select(cb.sum(root.<BigDecimal> get(Transaction_.TOTAL_BILLING_AMT)))
.where(tranSpec.toPredicate(root, query, cb));
final TypedQuery<BigDecimal> typedQuery = entityManager.createQuery(query);
return typedQuery.getSingleResult();
}
我曾考虑过使用子查询来获取不同的Transaction对象,但无法弄清楚如何在初始查询中使用子查询。
Subquery<Transaction> subquery = query.subquery(Transaction.class);
Root<Transaction> subroot = query.from(Transaction.class);
Specification<Transaction> tranSpec = TransactionSpecification.webFilteringSearch(request.getUserLogNo(), request.getRefNo(),
request.getBillingDateFrom(), request.getBillingDateTo(), request.getTransactionDateFrom(), request.getTransactionDateTo(),
request.getFund(), request.getMinTotalChargeAmt(), request.getMaxTotalChargeAmt(), request.getStatus(), request.getCardId(),
request.getUserId(), request.getCompanyName(), request.getCenters());
subquery.select(subroot)
.where(tranSpec.toPredicate(root, query, cb));
我尝试了几种在主查询中使用子查询的方法,但没有成功。
有人知道在查询中使用子查询的方法吗?