hibernate sql

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;











你可能感兴趣的:(sql,Hibernate)