今天在敲的时候,遇到一个bug:Property 'cust_name' not found on type java.lang.Long。在项目的jsp页面引用了“cust_name”,说找不到这个属性,开始一直以为是jsp哪里出错了,浪费半天时间也没找到原因,后来在代码里面找,发现在dao里调用的find()(查询所有方法)返回值是一个数字,而不是我需要的集合,并且这个数字是dao.find()上面一个方法的返回结果。想想可能是我方法里调用的DetachedCriteria的api不够属性,导致出错了,再网上查资料,返现调用一次DetachedCriteria的聚合函数方法后,需要把DetachedCriteria清空一下。代码:dc.setProjection(null);
原dao里的两个方法如下:
public Integer getCustomerNum(DetachedCriteria dc) {
dc.setProjection(Projections.rowCount());//设置查询的聚合函数
List<Long> list = (List<Long>) getHibernateTemplate().findByCriteria(dc);
//清空dc中的聚合函数,不然影响下次调用dc查询
dc.setProjection(null);
if(list!=null && list.size()>0){
return list.get(0).intValue();
}else{
return null;
}
}
public List<Customer> getCustomerList(final DetachedCriteria dc,
Integer start, Integer pageSize) {
List<Customer> list = (List<Customer>) getHibernateTemplate().findByCriteria(dc, start, pageSize);
return list;
}
网友评论