我正在尝试使用条件(Criteria Builder和CriteriaQuery)获得金额总和
但是我在编写代码时遇到错误; 这是我的代码
public Long findAmountByCriteria(OrderCriteria orderCriteria) {
final CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
final CriteriaQuery<BigDecimal> query = criteriaBuilder.createQuery(BigDecimal.class);
Root<Order> root = query.from(Order.class);
query.select(criteriaBuilder.sum(root.get("amount")))
.where(buildPredicateForOrderResponseCriteria(criteriaBuilder, orderCriteria, root));
final TypedQuery<BigDecimal> typedQuery = entityManager.createQuery(query);
return typedQuery.getSingleResult().longValue();
}
金额是BigDecimal值。
在选择mehtod中说,
The method select(Selection<? extends BigDecimal>) in the type CriteriaQuery<BigDecimal> is not applicable for the arguments (Expression<Object>)
总和方法
Bound mismatch: The generic method sum(Expression<N>) of type CriteriaBuilder is not applicable for the arguments (Path<Object>). The inferred type Object is not a valid substitute for the bounded parameter <N extends Number>
如何解决此问题。
答案 0 :(得分:1)
而不是你正在做的事情
query.select(criteriaBuilder.sum(root.get("amount")))
尝试这样做
query.select(criteriaBuilder.sum(root.<BigDecimal> get("amount")))