一次项目中使用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聚合查询
时间: 2024-10-16 15:23:34