问题描述
- Spring JPA CriteriaQuery分组查询
-
Spring JPA CriteriaQuery 设置select 查询的字段丝毫没起到作用,很郁闷、网上也有很多类似的问题,都没有解决方法。有没有大神用JPA做过分组查询的?求教。。。
解决方案
CriteriaQuery q = cb.createQuery(Country.class);
Root c = q.from(Country.class);
q.select(c.get(""currency"") cb.sum(c.get(""population"")));
q.where(cb.isMember(""Europe"" c.get(""continents"")));
q.groupBy(c.get(""currency""));
g.having(cb.gt(cb.count(c) 1));
对应的sql 语句SELECT c.currency SUM(c.population)
FROM Country c
WHERE 'Europe' MEMBER OF c.continents
GROUP BY c.currency
HAVING COUNT(c) > 1
api地址:http://www.objectdb.com/java/jpa/query/jpql/group#GROUP_BY_and_HAVING_in_Criteria_Queries_
时间: 2024-09-17 03:51:33