DetachedCriteria 分页Projection取消聚合条件
2011-11-13 16:05 java线程例子 阅读(2687) 评论(0) 编辑 收藏 举报
@Override
public PageBean findPaginated(DetachedCriteria detachedCriteria,PageBean pageBean) {
// TODO 今天记得修改 去掉DetachedCriteria中的projection
//根据该条件获得总记录数
Long total=this.getRowCount(detachedCriteria);
List<T> datas=this.findByCriteria(detachedCriteria, pageBean.getOffset(), pageBean.getPageSize());
pageBean.setDatas(datas);
pageBean.setTotal(total);
return pageBean;
}
// 使用指定的检索标准获取满足标准的记录数
public Long getRowCount(final DetachedCriteria detachedCriteria) {
return getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria=detachedCriteria.getExecutableCriteria(session);
CriteriaImpl criteriaImpl=(CriteriaImpl) criteria;
Projection projection=criteriaImpl.getProjection();
logger.debug("SQL:"+Projections.rowCount());
Long totalCount=(java.lang.Long) criteria.setProjection(Projections.rowCount()).uniqueResult();
criteria.setProjection(projection);
if(projection==null){
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
}
return totalCount;
}
});
public PageBean findPaginated(DetachedCriteria detachedCriteria,PageBean pageBean) {
// TODO 今天记得修改 去掉DetachedCriteria中的projection
//根据该条件获得总记录数
Long total=this.getRowCount(detachedCriteria);
List<T> datas=this.findByCriteria(detachedCriteria, pageBean.getOffset(), pageBean.getPageSize());
pageBean.setDatas(datas);
pageBean.setTotal(total);
return pageBean;
}
// 使用指定的检索标准获取满足标准的记录数
public Long getRowCount(final DetachedCriteria detachedCriteria) {
return getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria criteria=detachedCriteria.getExecutableCriteria(session);
CriteriaImpl criteriaImpl=(CriteriaImpl) criteria;
Projection projection=criteriaImpl.getProjection();
logger.debug("SQL:"+Projections.rowCount());
Long totalCount=(java.lang.Long) criteria.setProjection(Projections.rowCount()).uniqueResult();
criteria.setProjection(projection);
if(projection==null){
criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
}
return totalCount;
}
});
}
public List findByCriteria(DetachedCriteria criteria, int firstResult,
int maxResults) {
return getHibernateTemplate().findByCriteria(criteria, firstResult,
maxResults);
}
这个问题纠结我好长时间,每次查询出来总行数后,在查询就不能查询实体的信息了即使setProjectinos为null,但是查询出来的结果在有关联的情况下是List<Object[]>类型的
也就是说DetachedCriteria本身还是有问题,具体的原因我说不出来.但是这也是一种解决方案.希望对大家也有所帮助,
这样就可以将DetachedCriteria进行封装,在分页条件下查询总行数,