SQLQuery 还有很多使用价值,例如在处理非 hbm 对象的时候。
1, addScalar 显式指定返回数据的类型;以下是代码片段~~
String sql = "select count(*) as cnt from tblRead" + Misc.formatMoon(year, month) ... SQLQuery query = session.createSQLQuery(sql);query.addScalar("cnt", Hibernate.INTEGER);...count = (Integer) query.uniqueResult(); |
2, 返回的结果集是多个列的情况,以下是代码片段~~
String sql = "select day, count(*) as cnt from tblRead" + Misc.formatMoon(year, month) ... SQLQuery query = session.createSQLQuery(sql);query.addScalar("day", Hibernate.INTEGER);query.addScalar("cnt", Hibernate.INTEGER);...Iterator results = query.list().iterator();while (results.hasNext()){ Object[] rows = (Object[]) results.next(); int day = (Integer) rows[0]; int cnt = (Integer) rows[1]; ...} |