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_