String query = "select u.buyer_id, count(qu.id) from users u, Quotation qu where u.id = qu.buyer_user_id group by u.buyer_id";
Query qu = getSession().createSQLQuery(query);
List<Object[]> res = qu.list();
Map<Long, Long> map = new HashMap<Long, Long>();
for(Object[] item : res){
map.put(((BigInteger)item[0]).longValue(), ((BigInteger)item[1]).longValue());
}
return map;
////////////////////////////////////////
Criteria crit = getSession().createCriteria(Quotation.class, "q")
.createCriteria("user", "u", Criteria.LEFT_JOIN)
.add(Restrictions.eqProperty("q.user.id", "u.id"));
ProjectionList projList = Projections.projectionList();
projList.add(Projections.groupProperty("u.buyer.id"));
projList.add(Projections.count("q.id"));
crit.setProjection(projList);
List<Object[]> res = crit.list();
Map<Long, Long> map = new HashMap<Long, Long>();
for(Object[] item : res){
map.put(((Long)item[0]).longValue(), ((Integer)item[1]).longValue());
}
return map;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
String query = "select buyer_id, count(bill_id) from buyer_billing group by buyer_id";
Query qu = getSession().createSQLQuery(query);
List<Object[]> res = qu.list();
Map<Long, Long> map = new HashMap<Long, Long>();
for(Object[] item : res){
map.put(((BigInteger)item[0]).longValue(), ((BigInteger)item[1]).longValue());
}
return map;
////////////////////////////////////////
Criteria crit = getSession().createCriteria(BuyerBill.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.groupProperty("buyerId"));
projList.add(Projections.count("id"));
crit.setProjection(projList);
List<Object[]> res = crit.list();
Map<Long, Long> map = new HashMap<Long, Long>();
for(Object[] item : res){
map.put(((Long)item[0]).longValue(), ((Integer)item[1]).longValue());
}
return map;
Query qu = getSession().createSQLQuery(query);
List<Object[]> res = qu.list();
Map<Long, Long> map = new HashMap<Long, Long>();
for(Object[] item : res){
map.put(((BigInteger)item[0]).longValue(), ((BigInteger)item[1]).longValue());
}
return map;
////////////////////////////////////////
Criteria crit = getSession().createCriteria(Quotation.class, "q")
.createCriteria("user", "u", Criteria.LEFT_JOIN)
.add(Restrictions.eqProperty("q.user.id", "u.id"));
ProjectionList projList = Projections.projectionList();
projList.add(Projections.groupProperty("u.buyer.id"));
projList.add(Projections.count("q.id"));
crit.setProjection(projList);
List<Object[]> res = crit.list();
Map<Long, Long> map = new HashMap<Long, Long>();
for(Object[] item : res){
map.put(((Long)item[0]).longValue(), ((Integer)item[1]).longValue());
}
return map;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
String query = "select buyer_id, count(bill_id) from buyer_billing group by buyer_id";
Query qu = getSession().createSQLQuery(query);
List<Object[]> res = qu.list();
Map<Long, Long> map = new HashMap<Long, Long>();
for(Object[] item : res){
map.put(((BigInteger)item[0]).longValue(), ((BigInteger)item[1]).longValue());
}
return map;
////////////////////////////////////////
Criteria crit = getSession().createCriteria(BuyerBill.class);
ProjectionList projList = Projections.projectionList();
projList.add(Projections.groupProperty("buyerId"));
projList.add(Projections.count("id"));
crit.setProjection(projList);
List<Object[]> res = crit.list();
Map<Long, Long> map = new HashMap<Long, Long>();
for(Object[] item : res){
map.put(((Long)item[0]).longValue(), ((Integer)item[1]).longValue());
}
return map;
本文通过具体的SQL和HQL查询代码示例,展示了如何使用分组查询来获取特定的数据统计结果。其中包括了直接使用SQL进行查询的方式,以及利用Hibernate Query Language (HQL)配合Criteria API实现相同目标的方法。
2万+

被折叠的 条评论
为什么被折叠?



