一次项目中使用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();