读书人

模糊查询查询总数时报空指针错误

发布时间: 2012-04-26 14:01:31 作者: rapoo

模糊查询,查询总数时,报空指针异常
public Pagemodel FindUserLike(String keword, int offset, int pagesize) throws SQLException{
System.out.println("keword=="+keword);
String hql = "from User as u where u.userName like '"+keword+"'";
Query query = this.getSession().createQuery(hql);
//query.setString(0, "%"+keword+"%");
int total = ((Long)query.uniqueResult()).intValue();
System.out.println("total:" + total);
List datas = this.getSession().createQuery("from User")
.setFirstResult(offset)
.setMaxResults(pagesize).list();

//得到结果集
Pagemodel pm = new Pagemodel();
pm.setTotal(total);
pm.setDatas(datas);
return pm;
}

[解决办法]

探讨
问题解决了,查询时的对象不明确,hql中加上select count(*)就对了,这个小问题搞了一下午。悲剧啊!

读书人网 >Java相关

热点推荐