Example usage for org.hibernate Criteria setFirstResult

List of usage examples for org.hibernate Criteria setFirstResult

Introduction

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

Prototype

public Criteria setFirstResult(int firstResult);

Source Link

Document

Set the first result to be retrieved.

Usage

From source file:com.abiquo.server.core.enterprise.RoleLdapDAO.java

License:Open Source License

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

    Long total = count(criteria);

    criteria = createCriteria(filter, orderBy, desc);

    criteria.setFirstResult(offset * numResults);
    criteria.setMaxResults(numResults);/*  w w w  .  j av  a2  s  . c o  m*/

    List<RoleLdap> result = getResultList(criteria);

    PagedList<RoleLdap> page = new PagedList<RoleLdap>();
    page.addAll(result);
    page.setCurrentElement(offset);
    page.setPageSize(numResults);
    page.setTotalResults(total.intValue());

    return page;
}

From source file:com.abiquo.server.core.enterprise.UserDAO.java

License:Open Source License

public Collection<User> find(final Enterprise enterprise, final Role role, final String filter,
        final String orderBy, final boolean desc, final boolean connected, final Integer offset,
        final Integer numResults) {
    String[] filters = new String[] {};

    if (filter != null) {
        filters = filter.split("\\s+");
    }//from   ww w  .  ja  v a2 s .  c o  m
    Criteria criteria = createCriteria(enterprise, role, filters, orderBy, desc, connected);
    Long total = count(criteria);
    criteria = createCriteria(enterprise, role, filters, orderBy, desc, connected);

    criteria.setFirstResult(offset * numResults);
    criteria.setMaxResults(numResults);

    List<User> result = getResultList(criteria);

    PagedList<User> page = new PagedList<User>();
    page.addAll(result);
    page.setCurrentElement(offset);
    page.setPageSize(numResults);
    page.setTotalResults(total.intValue());

    return page;
}

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

License:Open Source License

public Collection<CostCode> 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   ww  w.j ava2 s  . co m*/

    List<CostCode> result = getResultList(criteria);

    PagedList<CostCode> page = new PagedList<CostCode>();
    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.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 ww  .j  a  va  2  s  .  c  o  m

    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 ww w  .  j  a v a2s  .com*/

    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,./*  ww  w.j ava 2 s.  c o  m*/
 */
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  . j a  v  a2s  . co  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//from w  w w  .j a v  a  2s.  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 .  java2  s  .  co  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 .jav  a  2  s  .  c om
 * @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;
}