如何使用 JPA 使用 AVG 和 GROUP 创建条件查询

How to Create a Criteria Query with AVG and GROUP using JPA

我想使用 Criteria 查询获取基于平均客户评分的产品列表。我使用标准查询聚合函数 AVG 编写了一段代码并保留了 GROUP BY 子句。但不知何故,我得到 "Not a Group BY Expression" 异常。我尝试并在 Google 中搜索以解决问题,但没有任何帮助。在下面发布代码

CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Object[]> cq = builder.createQuery(Object[].class);
        Root<GdbCustomerProductReviewImpl> productReview = cq.from(GdbCustomerProductReviewImpl.class);
        Path productPath = productReview.get("product");
        Path documentPath = productPath.get("photo");
        Path categoryPath = productPath.get("defaultCategory");
        Path productCategoryPath = categoryPath.get("prdCategory");
        cq.multiselect(productReview.get("id"),productPath.get("url"),productPath.get("skuName"),documentPath.get("id"));
        cq.where(builder.isNotNull(productPath.get("id")));
        cq.where(builder.equal(productReview.get("status"),"ACTIVE"));
        cq.where(builder.equal(productReview.get("reviewType"),"PRODUCT"));
        cq.where(builder.equal(productPath.get("isEnable"),Boolean.TRUE));
        cq.where(builder.equal(productPath.get("status"),StatusType.APPROVED.getType()));
        cq.where(builder.isNotNull(productPath.get("defSkuMap")));
        cq.where(builder.equal(productCategoryPath.get("isEnabled"),Boolean.TRUE));
        cq.groupBy(productReview.get("id"));
        Expression event_count = builder.avg(productReview.get("rating"));
        cq.orderBy(builder.desc(event_count));
        List<Object[]> resultList = em.createQuery(cq).setMaxResults(size).getResultList();

在进一步研究中找到了解决方案,也感谢 crizzis。发布对我有用的解决方案。

CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Object[]> cq = builder.createQuery(Object[].class);
        Root<GdbCustomerProductReviewImpl> productReview = cq.from(GdbCustomerProductReviewImpl.class);
        Path productPath = productReview.get("product");
        Path documentPath = productPath.get("photo");
        Path categoryPath = productPath.get("defaultCategory");
        Path productCategoryPath = categoryPath.get("prdCategory");
        Expression event_count = builder.avg(productReview.get("rating"));
        cq.multiselect(productPath.get("id"),productPath.get("url"),productPath.get("skuName"),documentPath.get("id"));
        cq.where(builder.isNotNull(productPath.get("id")),builder.equal(productReview.get("status"),"ACTIVE"),builder.equal(productReview.get("reviewType"),"PRODUCT"),builder.equal(productPath.get("isEnable"),Boolean.TRUE),builder.equal(productPath.get("status"),StatusType.APPROVED.getType()),builder.isNotNull(productPath.get("defSkuMap")),builder.equal(productCategoryPath.get("isEnabled"),Boolean.TRUE));
        cq.groupBy(productPath.get("id"),productPath.get("url"),productPath.get("skuName"),documentPath.get("id"));
       
        cq.orderBy(builder.desc(event_count));
        List<Object[]> resultList = em.createQuery(cq).setMaxResults(size).getResultList();