Example usage for org.hibernate Criteria setMaxResults

List of usage examples for org.hibernate Criteria setMaxResults

Introduction

In this page you can find the example usage for org.hibernate Criteria setMaxResults.

Prototype

public Criteria setMaxResults(int maxResults);

Source Link

Document

Set a limit upon the number of objects to be retrieved.

Usage

From source file:ch.qos.logback.audit.persistent.AuditEventDAO.java

License:Open Source License

@SuppressWarnings("unchecked")
static public List<Object[]> findMaxObject() throws HibernateException {
    Session s = null;/*from  ww w. ja v a2  s  . c  om*/
    try {
        s = openSession();

        //Query q = s.createQuery("select count(ae.subject), ae.subject FROM "+ AE_CLASS + " AS ae "+
        //"GROUP BY ae.subject ORDER BY count(ae.subject) desc");

        Criteria crit = s.createCriteria(AuditEvent.class);
        crit.setProjection(Projections.projectionList().add(Projections.count("object").as("gcount"))
                .add(Projections.groupProperty("object")));
        crit.addOrder(Order.desc("gcount"));
        crit.setMaxResults(10);
        return crit.list();
    } finally {
        close(s);
    }
}

From source file:cn.com.jandar.lawyer.service.LawFirmService.java

@SuppressWarnings("unchecked")
public List<LawFirm> findByParam(Date lastUpdateDate, Integer pageNum, Integer pageSize) {
    Criteria c = lawFirmDao.getSession().createCriteria(LawFirm.class);
    if (lastUpdateDate != null) {
        c.add(Restrictions.ge("lastUpdateDate", lastUpdateDate));
    }/*from  w  ww. j  ava 2s .  com*/
    c.setMaxResults(pageSize);
    c.setFirstResult(pageNum * pageSize + 1);
    c.addOrder(Order.desc("laseUpdateDate"));
    return c.list();
}

From source file:cn.com.jandar.lawyer.service.LawyerService.java

/**
 * ?//from w  w  w  . ja va  2 s  .com
 * @param lastUpdateDate
 * @param pageNum
 * @param pageSize
 * @return
 */
@SuppressWarnings("unchecked")
public List<Lawyer> findByParam(Date lastUpdateDate, Integer pageNum, Integer pageSize) {
    Criteria c = lawyerDao.getSession().createCriteria(Lawyer.class);
    if (lastUpdateDate != null) {
        c.add(Restrictions.ge("lastUpdateDate", lastUpdateDate));
    }
    c.setMaxResults(pageSize);
    c.setFirstResult(pageNum * pageSize + 1);
    c.addOrder(Order.desc("laseUpdateDate"));
    return c.list();
}

From source file:cn.dayuanzi.dao.BaseDao.java

License:Apache License

/**
 * ?/*from  ww w .  j a  v a  2  s. c o m*/
 * 
 * @param current_page >=1
 * @param max_num >=1 <=1000
 * @param map
 * @param orderByProperty
 * @param isAsc
 * @return
 */
public List<T> findForPage(int current_page, int max_num, Map<String, Object> map, String orderByProperty,
        boolean isAsc) {
    List<T> result = null;
    if (current_page <= 0) {
        current_page = 1;
    }
    if (max_num > 20) {
        max_num = 20;
    } else if (max_num <= 0) {
        max_num = 1;
    }
    Criteria c = createCriteria();
    if (MapUtils.isNotEmpty(map)) {
        Iterator<String> it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            c.add(Restrictions.eq(key, map.get(key)));
        }
    }
    c.setFirstResult(((current_page - 1)) * max_num);
    c.setMaxResults(max_num);
    if (StringUtils.isNotBlank(orderByProperty)) {
        if (isAsc) {
            c.addOrder(Order.asc(orderByProperty));
        } else {
            c.addOrder(Order.desc(orderByProperty));
        }
    }
    result = c.list();
    return result;
}

From source file:cn.dayuanzi.dao.BaseDao.java

License:Apache License

public List<T> findForPage(int current_page, int max_num, String orderByProperty, boolean isAsc,
        final Criterion... criterions) {
    List<T> result = null;//  w w  w  .  ja va 2s .  com
    if (current_page <= 0) {
        current_page = 1;
    }
    if (max_num > 20) {
        max_num = 20;
    } else if (max_num <= 0) {
        max_num = 1;
    }
    Criteria c = createCriteria(criterions);
    c.setFirstResult(((current_page - 1)) * max_num);
    c.setMaxResults(max_num);
    if (StringUtils.isNotBlank(orderByProperty)) {
        if (isAsc) {
            c.addOrder(Order.asc(orderByProperty));
        } else {
            c.addOrder(Order.desc(orderByProperty));
        }
    }
    result = c.list();
    return result;
}

From source file:cn.hxh.springside.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * ?Criteria,.//  w ww  .  j  a v  a2  s. com
 */
protected Criteria setPageRequestToCriteria(final Criteria c, final PageRequest pageRequest) {
    AssertUtils.isTrue(pageRequest.getPageSize() > 0, "Page Size must larger than zero");

    c.setFirstResult(pageRequest.getOffset());
    c.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.isOrderBySetted()) {
        for (Sort sort : pageRequest.getSort()) {
            if (Sort.ASC.equals(sort.getDir())) {
                c.addOrder(Order.asc(sort.getProperty()));
            } else {
                c.addOrder(Order.desc(sort.getProperty()));
            }
        }
    }
    return c;
}

From source file:cn.lhfei.airqa.dao.support.Hibernate4DaoSupport.java

License:Apache License

@Override
public Page<E> findPageByCriteria(Criterion criterion, Page<E> page) {
    Criteria criteria = getCurrentSession().createCriteria(entityClass);
    int count = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();
    criteria.setProjection(null);/*from   w w  w  .  j a  v  a  2 s. c  o  m*/
    if (criterion != null) {
        criteria.add(criterion);
    }
    page.setTotalCount(count);
    int startCount = (page.getCurPage() - 1) * page.getPageSize();
    int endCount = startCount + page.getPageSize();
    criteria.setFirstResult(startCount);
    criteria.setMaxResults(endCount);
    page.setResult(criteria.list());
    return page;
}

From source file:cn.newtouch.util.hibernate.HibernateDao.java

License:Apache License

/**
 * ?Criteria,./*from  www  .java 2  s .  c  om*/
 */
protected Criteria setPageParameterToCriteria(final Criteria c, final Page<T> page) {

    Assert.isTrue(page.getPageSize() > 0, "Page Size must larger than zero");

    // hibernatefirstResult??0
    c.setFirstResult(page.getFirst() - 1);
    c.setMaxResults(page.getPageSize());

    if (page.isOrderBySetted()) {
        String[] orderByArray = StringUtils.split(page.getOrderBy(), ',');
        String[] orderArray = StringUtils.split(page.getOrder(), ',');

        Assert.isTrue(orderByArray.length == orderArray.length,
                "???,????");

        for (int i = 0; i < orderByArray.length; i++) {
            if (Page.ASC.equals(orderArray[i])) {
                c.addOrder(Order.asc(orderByArray[i]));
            } else {
                c.addOrder(Order.desc(orderByArray[i]));
            }
        }
    }
    return c;
}

From source file:cn.newtouch.util.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * ?Criteria,./*from   w  w  w.  j av a  2s  .  co m*/
 */
protected Criteria setPageParameterToCriteria(final Criteria c, final Page<T> page) {

    Assert.isTrue(page.getPageSize() > 0, "Page Size must larger than zero");

    //hibernatefirstResult??0
    c.setFirstResult(page.getFirst() - 1);
    c.setMaxResults(page.getPageSize());

    if (page.isOrderBySetted()) {
        String[] orderByArray = StringUtils.split(page.getOrderBy(), ',');
        String[] orderArray = StringUtils.split(page.getOrder(), ',');

        Assert.isTrue(orderByArray.length == orderArray.length,
                "???,????");

        for (int i = 0; i < orderByArray.length; i++) {
            if (Page.ASC.equals(orderArray[i])) {
                c.addOrder(Order.asc(orderByArray[i]));
            } else {
                c.addOrder(Order.desc(orderByArray[i]));
            }
        }
    }
    return c;
}

From source file:cn.trymore.core.dao.impl.DAOGenericImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public PaginationSupport<T> findPageByCriteria(final DetachedCriteria criteria, final int pageSize,
        final int startIndex, final boolean dataFilter) throws DAOException {
    if (dataFilter && UtilString.isNotEmpty(this.getQueryFilter())) {
        criteria.add(Restrictions.sqlRestriction(this.getQueryFilter()));
        this.setQueryFilter(null);
    }/*  w  w w.  j a v a2  s  .c  o  m*/

    return (PaginationSupport<T>) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria execCriteria = criteria.getExecutableCriteria(session);

            int rowCount = ((Integer) execCriteria.setProjection(Projections.rowCount()).uniqueResult())
                    .intValue();
            execCriteria.setProjection(null);
            execCriteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            //execCriteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
            execCriteria.setFirstResult(startIndex);

            if (pageSize > 0) {
                execCriteria.setMaxResults(pageSize);
            } else {
                execCriteria.setMaxResults(rowCount);
            }
            List<T> items = execCriteria.list();
            return rowCount > 0
                    ? new PaginationSupport<T>(items, rowCount, startIndex, pageSize > 0 ? pageSize : rowCount)
                    : null;
        }
    });
}