在每次执行的时候,都重新构造一次Criteria对象

解决方案 »

  1.   

    action:
         Criteria criteria = 
    positionManager.createCriteria(SysPosition.class);
    if ((this.getPositionName() != null)
    && (this.getPositionName().length() > 0)) {
    criteria.add(Expression.like("positionName","%"+this.getPositionName()+"%"));

    }
     

        Page page = positionManager.pagedQuery(criteria, param.getCurPage(), param.getPageSize());

        this.getRequest().put("positionList", page.getResult());
    HibernateGenericDao
       public Page pagedQuery(Criteria criteria, int pageNo, int pageSize) {
    Assert.notNull(criteria);
    Assert.isTrue(pageNo >= 1, "pageNo should start from 1");
    CriteriaImpl impl = (CriteriaImpl) criteria;

    Projection projection = impl.getProjection();

    List<CriteriaImpl.OrderEntry> orderEntries;
    try {
    orderEntries = (List) BeanUtils.forceGetProperty(impl, "orderEntries");
    BeanUtils.forceSetProperty(impl, "orderEntries", new ArrayList());
    } catch (Exception e) {
    throw new InternalError(" Runtime Exception impossibility throw ");
    }
        

    int totalCount = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();



    criteria.setProjection(projection);
    if (projection == null) {
    criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
    } try {
    BeanUtils.forceSetProperty(impl, "orderEntries", orderEntries);
    } catch (Exception e) {
    throw new InternalError(" Runtime Exception impossibility throw ");
    }
    if (totalCount < 1)
    return new Page(); int startIndex = Page.getStartOfPage(pageNo, pageSize);
    List list = criteria.setFirstResult(startIndex).setMaxResults(pageSize).list();
    criteria.setProjection(null);
    return new Page(startIndex, totalCount, pageSize, list);
    }
      

  2.   

    改用DetachedCriteria吧
    这有个不错的例子
    http://www.javaeye.com/topic/14657