addScalar("b03yf", Hibernate.STRING)中的“b03yf”是Hibernate的string类型。可以使用addScalar方法将查询结果转换成标量值,如:
Double max = (Double) session.createSQLQuery("select max(cat.weight) as maxWeight from cats cat") .addScalar("maxWeight", Hibernate.DOUBLE).uniqueResult();
注意Hibernate.***,***是要全部大写的。
public int queryByAdvCount(String stWindow,……String dtFinish2) {
StringBuffer sql = new StringBuffer("select count(st_apply_id) total from inf_apply WHERE 1=1");
if (!"choose".equals(stWindow)) {
sql.append(" AND st_window='"+stWindow+"'");
}
……
SQLQuery q = getSession().createSQLQuery(sql.toString()).addScalar("total", Hibernate.INTEGER);
int count = (Integer) q.list().get(0);
return count;
}