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:com.abiquo.server.core.pricing.CurrencyDAO.java

License:Open Source License

public Collection<Currency> find(final String filter, final String orderBy, final boolean desc,
        final int offset, int numResults) {
    Criteria criteria = createCriteria(filter, orderBy, desc);

    Long total = count(criteria);

    criteria = createCriteria(filter, orderBy, desc);
    numResults = (int) (numResults != 0 ? numResults : total);
    if (numResults != 0) {
        criteria.setFirstResult(offset * numResults);
        criteria.setMaxResults(numResults);
    }/*from  w  w w.  ja v  a2  s .com*/

    List<Currency> result = getResultList(criteria);

    PagedList<Currency> page = new PagedList<Currency>();
    page.addAll(result);
    if (numResults != 0) {
        page.setCurrentElement(offset);
        page.setPageSize(numResults);
    }
    page.setTotalResults(total.intValue());

    return page;
}

From source file:com.abiquo.server.core.pricing.PricingTemplateDAO.java

License:Open Source License

public Collection<PricingTemplate> find(final String filter, final String orderBy, final boolean desc,
        final Integer offset, Integer numResults, final Integer startwith) {
    Criteria criteria = createCriteria(filter, orderBy, desc);

    Long total = count(criteria);

    criteria = createCriteria(filter, orderBy, desc);
    numResults = (int) (numResults != 0 ? numResults : total);
    criteria.setFirstResult(offset * numResults);
    if (startwith != -1) {
        criteria.setFirstResult(startwith);
    }//from w  ww . ja  v  a2s.c  o m

    criteria.setMaxResults(numResults);

    List<PricingTemplate> result = getResultList(criteria);

    PagedList<PricingTemplate> page = new PagedList<PricingTemplate>();
    page.addAll(result);
    page.setCurrentElement(offset);
    if (startwith != -1) {
        page.setCurrentElement(startwith);
    }
    page.setPageSize(numResults);
    page.setTotalResults(total.intValue());

    return page;
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 * ?Criteria,./*from   www  . j  ava  2s .c  om*/
 */
protected Criteria setPageParameter(final Criteria c, final Page<T> page) {
    // hibernatefirstResult??0
    if (page.getPageSize() > 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, "orderBy and order is not suited!");

        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:com.abssh.util.GenericDao.java

License:Apache License

@SuppressWarnings("unchecked")
public Page<T> findPageDynamicFetch(final Page<T> page, final List<PropertyFilter> filters,
        final String... lazyObjects) {
    Criteria criteria = getSession().createCriteria(entityClass);
    Map<String, Criteria> criteriaMap = new HashMap<String, Criteria>();
    for (PropertyFilter filter : filters) {
        if (!MatchType.INS.equals(filter.getMatchType())) {
            if (!filter.isMultiProperty()) {
                String propertyName = filter.getPropertyName();
                Object[] propertyValue = filter.getPropertyValue();
                MatchType matchType = filter.getMatchType();
                Criteria parent = findParentCriteria(criteria, propertyName, criteriaMap);
                String[] tmp = StringUtils.split(propertyName, DEF_SEPARATOR);
                parent.add(getCriterion(tmp[tmp.length - 1], propertyValue, matchType));
            } else {
                Disjunction disjunction = Restrictions.disjunction();
                Object[] propertyValue = filter.getPropertyValue();
                MatchType matchType = filter.getMatchType();
                String[] propertyNames = filter.getPropertyNames();
                for (String propertyName : propertyNames) {
                    // Criteria parent = findParentCriteria(criteria,
                    // propertyName, criteriaMap);
                    String[] tmp = StringUtils.split(propertyName, DEF_SEPARATOR);
                    // parent.add(getCriterion(tmp[tmp.length - 1],
                    // propertyValue, matchType));
                    for (int i = 0; i <= tmp.length - 2; i++) {
                        criteria.createAlias(tmp[i], tmp[i], CriteriaSpecification.LEFT_JOIN);
                    }//from w ww .ja  v a 2  s . c  o m
                    // disjunction.ad
                }
                criteria.add(Restrictions.or(
                        Restrictions.like(propertyNames[0], propertyValue[0].toString(), MatchMode.ANYWHERE),
                        Restrictions.like(propertyNames[1], propertyValue[0].toString(), MatchMode.ANYWHERE)));

                // criteria.add(disjunction);
            }
        } else {
            criteria.add(org.hibernate.criterion.Expression.sql("this_." + filter.getPropertyName() + " in "
                    + String.valueOf(filter.getPropertyValue()[0])));
        }
    }
    if (lazyObjects != null) {
        for (int i = 0; i < lazyObjects.length; i++) {
            criteria.setFetchMode(lazyObjects[i], FetchMode.EAGER);
        }
    }
    if (page != null && page.isAutoCount()) {
        int totalCount = countCriteriaResult(criteria);
        page.setTotalCount(totalCount);
    }
    if (page != null && page.getPageSize() > 0) {
        if (page.getTotalPages() < page.getPageNo()) {
            page.setPageNo(1L);
        }
        criteria.setFirstResult(page.getFirst() - 1);
        criteria.setMaxResults(page.getPageSize());
    }
    if (page != null && page.isOrderBySetted()) {
        String[] orderByArray = StringUtils.split(page.getOrderBy(), ',');
        String[] orderArray = StringUtils.split(page.getOrder(), ',');

        Assert.isTrue(orderByArray.length == orderArray.length, "orderBy and order is not suited!");

        for (int i = 0; i < orderByArray.length; i++) {
            if (orderByArray[i].indexOf(".") > 0) {
                // ???
                if (Page.ASC.equals(orderArray[i])) {
                    Criteria p = criteriaMap
                            .get(orderByArray[i].substring(0, orderByArray[i].lastIndexOf(".")));
                    if (p == null) {
                        p = findParentCriteria(criteria, orderByArray[i], criteriaMap);// ??
                    }
                    p.addOrder(Order.asc(orderByArray[i].substring(orderByArray[i].lastIndexOf(".") + 1)));
                } else {
                    Criteria p = criteriaMap
                            .get(orderByArray[i].substring(0, orderByArray[i].lastIndexOf(".")));
                    if (p == null) {
                        p = findParentCriteria(criteria, orderByArray[i], criteriaMap);// ??
                    }
                    p.addOrder(Order.desc(orderByArray[i].substring(orderByArray[i].lastIndexOf(".") + 1)));
                }
            } else {
                if (Page.ASC.equals(orderArray[i])) {
                    criteria.addOrder(Order.asc(orderByArray[i]));
                } else {
                    criteria.addOrder(Order.desc(orderByArray[i]));
                }
            }
        }
    }

    List result = criteria.list();
    if (page == null) {
        Page p = new Page<T>();
        p.setResult(result);
        p.setTotalCount(result.size());
        p.setPageNo(1L);
        p.setPageSize(result.size());
        return p;
    }
    page.setResult(result);
    return page;
}

From source file:com.abssh.util.GenericDao.java

License:Apache License

/**
 *   OR//w  ww  . j  a v  a  2  s .  c o m
 * 
 * @new date 2012-04-23
 * @param page
 * @param filters
 * @return
 */
@SuppressWarnings("unchecked")
public Page<T> findPageWithOR(final Page<T> page, final List<PropertyFilter> filters) {
    Criteria criteria = getSession().createCriteria(entityClass);
    Map<String, Criteria> criteriaMap = new HashMap<String, Criteria>();

    for (PropertyFilter filter : filters) {
        if (!filter.isMultiProperty()) {
            String propertyName = filter.getPropertyName();
            createSubCriteria(criteria, propertyName, criteriaMap);
        } else {
            for (String propertyName : filter.getPropertyNames()) {
                createSubCriteria(criteria, propertyName, criteriaMap);
            }
        }
    }

    boundCriterion(criteria, buildPropertyFilterCriterions(filters));

    if (page != null && page.isAutoCount()) {
        int totalCount = countCriteriaResult(criteria);
        page.setTotalCount(totalCount);
    }
    if (page != null && page.getPageSize() > 0) {
        if (page.getTotalPages() < page.getPageNo()) {
            page.setPageNo(1L);
        }
        criteria.setFirstResult(page.getFirst() - 1);
        criteria.setMaxResults(page.getPageSize());
    }
    if (page != null && page.isOrderBySetted()) {
        String[] orderByArray = StringUtils.split(page.getOrderBy(), ',');
        String[] orderArray = StringUtils.split(page.getOrder(), ',');

        Assert.isTrue(orderByArray.length == orderArray.length, "orderBy and order is not suited!");

        for (int i = 0; i < orderByArray.length; i++) {
            if (orderByArray[i].indexOf(".") > 0) {
                // ???
                if (Page.ASC.equals(orderArray[i])) {
                    Criteria p = criteriaMap
                            .get(orderByArray[i].substring(0, orderByArray[i].lastIndexOf(".")));
                    if (p == null) {
                        p = findParentCriteria(criteria, orderByArray[i], criteriaMap);// ??
                    }
                    p.addOrder(Order.asc(orderByArray[i].substring(orderByArray[i].lastIndexOf(".") + 1)));
                } else {
                    Criteria p = criteriaMap
                            .get(orderByArray[i].substring(0, orderByArray[i].lastIndexOf(".")));
                    if (p == null) {
                        p = findParentCriteria(criteria, orderByArray[i], criteriaMap);// ??
                    }
                    p.addOrder(Order.desc(orderByArray[i].substring(orderByArray[i].lastIndexOf(".") + 1)));
                }
            } else {
                if (Page.ASC.equals(orderArray[i])) {
                    criteria.addOrder(Order.asc(orderByArray[i]));
                } else {
                    criteria.addOrder(Order.desc(orderByArray[i]));
                }
            }
        }
    }

    List result = criteria.list();
    if (page == null) {
        Page p = new Page<T>();
        p.setResult(result);
        p.setTotalCount(result.size());
        p.setPageNo(1L);
        p.setPageSize(result.size());
        return p;
    }
    page.setResult(result);
    return page;
}

From source file:com.age.core.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * ?Criteria,.//  w w  w  . j a v a2s .  c o m
 */
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());
    return c;
}

From source file:com.aistor.common.persistence.BaseDaoImpl.java

License:Open Source License

/**
 * /*from  w  w  w  .  j  a  v a2s  . c  o m*/
 * @param detachedCriteria
 * @param page
 * @return
 */
@SuppressWarnings("unchecked")
public Page<T> find(Page<T> page, DetachedCriteria detachedCriteria) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        page.setCount(count(detachedCriteria));
        if (page.getCount() < 1) {
            return page;
        }
    }
    Criteria criteria = detachedCriteria.getExecutableCriteria(getSession());
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);
    // set page
    if (!page.isDisabled()) {
        criteria.setFirstResult(page.getFirstResult());
        criteria.setMaxResults(page.getMaxResults());
    }
    // order by
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        for (String order : StringUtils.split(page.getOrderBy(), ",")) {
            String[] o = StringUtils.split(order, " ");
            if (o.length == 1) {
                criteria.addOrder(Order.asc(o[0]));
            } else if (o.length == 2) {
                if ("DESC".equals(o[1].toUpperCase())) {
                    criteria.addOrder(Order.desc(o[0]));
                } else {
                    criteria.addOrder(Order.asc(o[0]));
                }
            }
        }
    }
    page.setList(criteria.list());
    return page;
}

From source file:com.algoTrader.entity.marketData.BarDaoBase.java

/**
 * {@inheritDoc}//  w  w  w .  j  ava  2  s .c  om
 */
@Override
public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize) {
    try {
        final Criteria criteria = this.getSession(false).createCriteria(BarImpl.class);
        if (pageNumber > 0 && pageSize > 0) {
            criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            criteria.setMaxResults(pageSize);
        }
        final Collection<?> results = criteria.list();
        this.transformEntities(transform, results);
        return results;
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:com.algoTrader.entity.marketData.GenericMarketDataEventDaoBase.java

/**
 * {@inheritDoc}/*from   w  ww.j  a  v  a 2  s . c o  m*/
 */
@Override
public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize) {
    try {
        final Criteria criteria = this.getSession(false).createCriteria(GenericMarketDataEventImpl.class);
        if (pageNumber > 0 && pageSize > 0) {
            criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            criteria.setMaxResults(pageSize);
        }
        final Collection<?> results = criteria.list();
        this.transformEntities(transform, results);
        return results;
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}

From source file:com.algoTrader.entity.marketData.MarketDataEventDaoBase.java

/**
 * {@inheritDoc}/*from w w  w .j  a va  2  s  . co  m*/
 */
@Override
public Collection<?> loadAll(final int transform, final int pageNumber, final int pageSize) {
    try {
        final Criteria criteria = this.getSession(false).createCriteria(MarketDataEventImpl.class);
        if (pageNumber > 0 && pageSize > 0) {
            criteria.setFirstResult(this.calculateFirstResult(pageNumber, pageSize));
            criteria.setMaxResults(pageSize);
        }
        final Collection<?> results = criteria.list();
        this.transformEntities(transform, results);
        return results;
    } catch (HibernateException ex) {
        throw super.convertHibernateAccessException(ex);
    }
}