日期:2014-05-20  浏览次数:20810 次

模糊查询,查询总数时,报空指针异常
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(*)就对了,这个小问题搞了一下午。悲剧啊!