hibernate的 addScalar 方法是干什么用的?
hibernate的 addScalar 方法是干什么用的? 麻烦详细讲解一下 谢了!
------解决方案--------------------
http://blog.163.com/liujunjie51072/blog/static/8689162120086186455915/
请参考:
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];
...
}