How to use SUM aggregate in JPA Specification?

吃可爱长大的小学妹 提交于 2019-12-11 07:44:44

问题


I am trying to write a specification in order to restrict returned values of findAll() repository method. I can add groupBy and orderBy clause but cannot use sum aggregate (no errors but can't see aggregate result SUM_TOTAL in logged query).

here is my code:

public static Specification<Commande> searchCommandWith(final Long dId, final Long gId, final Long pId, final LocalDate date1, final LocalDate date2) {

    return new Specification<Commande>() {
        @Override
        public Predicate toPredicate(Root<Commande> commandeRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {

            final List<Predicate> predicates = new ArrayList<Predicate>();

            if(dId!=null && dId > 0 ) {
                 Predicate p = cb.equal(commandeRoot.get("d").get("id"), dId);
                 predicates.add(p);
            }

            if(gId!=null && gId > 0 ) {
                Predicate p = cb.equal(commandeRoot.get("g").get("id"), gId);
                predicates.add(p);
            }

            if(pId!=null && pId > 0 ) {
                Predicate p = cb.equal(commandeRoot.get("p").get("id"), pId);
                predicates.add(predicate3);
            }

            //TODO
            if(date1!=null && date2!=null ) {
                Path<LocalDate> date = commandeRoot.<LocalDate>get("date");
                Predicate startPredicate = cb.greaterThanOrEqualTo(date, date1);                
                Predicate endPredicate = cb.or(cb.isNull(date), cb.lessThanOrEqualTo(date, date2));
                Predicate p= cb.and(startPredicate, endPredicate);

                predicates.add(p);
            }     

            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        }             
    };
}

public static Specification<Commande> searchCommandWith(                                                    final String sumCol, final String groupBy, final String orderBy, ..) {

    return new Specification<Commande>() {
        @Override
        public Predicate toPredicate(Root<Commande> commandeRoot, CriteriaQuery<?> query, CriteriaBuilder cb) {             

            if(sumCol!=null && !sumCol.isEmpty()){

                Expression sumExp = cb.sum(commandeRoot.<Double>get(sumCol));
                query.select(sumExp.alias("SUM_TOTAL"));

            }

            if(groupBy!=null && !groupBy.isEmpty()){
                query.groupBy(commandeRoot.get(groupBy));
            }

            if(orderBy!=null && !orderBy.isEmpty()){
                //query.orderBy(commandeRoot.get(orderBy));
                query.orderBy(cb.desc(commandeRoot.get(orderBy)));
            }


            return searchCommandWith(delegueId, grossisteId, pharmacieId, dateDebut, dateFin).toPredicate(commandeRoot, query, cb);
        }             
    };

来源:https://stackoverflow.com/questions/25107536/how-to-use-sum-aggregate-in-jpa-specification

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!