一次项目中使用hibernate 的templet方式执行普通sql,方式如下:
String sql ="select count(*) as c from users";
SQLQuery sq = session.createSQLQuery(sql);
Object count = sq.uniqueResult();
结果报错column ‘‘ can not found
解决方法是设置返回列的类型,通过addScalar方法。如下所示:
String sql ="select count(*) as c from users";
SQLQuery sq = session.createSQLQuery(sql).addScalar("c", Hibernate.INTEGER);
Object count = sq.uniqueResult();
hibernate SQL聚合查询,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/BrightMoon/p/3832459.html