Hibernate中的addScalar用法
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();
把查询的值进行一定类型的转换
addScalar 返回的结果集是多个列的情况
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]; ... }
addScalar 返回到实体上
从一个表中查询极个别的字段并且还有几个是求多条数据的和的
"select sum(REAL_AMOUNT) as realAmount from order where uid=1"
SQLQuery query = getSession().createSQLQuery(buf.toString());
query.addScalar("realAmount", StandardBasicTypes.INTEGER);
query.setResultTransformer(Transformers.aliasToBean(ProductBillInfo.class));
List<ProductBillInfo> lnfo =query.list();
把和的结果存到一个字段里