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.tm.spring.dao.EmpDAOImpl.java

@Override
public List displayAll(int i) {
    List employees = new ArrayList();
    Session session = sessionFactory.openSession();
    Transaction t = session.beginTransaction();
    Criteria c = session.createCriteria(Employee.class);
    c.setFirstResult(i);
    c.setMaxResults(5);// w w w .j  a v  a2s .c om
    employees = c.list();
    t.commit();
    session.close();
    return employees;
}

From source file:com.uniminuto.ejercicio2.dao.EmployeeDaoImpl.java

public List<Employee> buscarSalarioRango(int menor, int mayor) {
    List<Employee> results = null;
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();//from  w  w  w. ja  v a2  s  .  c o m
    Criteria cr = session.createCriteria(Employee.class);
    //Trae entre 1 a 8 como maximo de registros
    cr.setFirstResult(1);
    cr.setMaxResults(8);
    cr.add(Restrictions.between("salario", menor, mayor));
    return results = cr.list();
}

From source file:com.viettel.model.GenericHibernateDAO.java

License:Open Source License

/**
 * find by Criterion with firstResult and maxResult limit This method may
 * throw a HibernateException/*  w  w  w  .  ja v  a  2 s . c om*/
 *
 * @param sessionName Name of hibernate session
 * @param firstResult The position of the first result to retrieve. Zero
 * mean this argument will be ignored
 * @param maxResult The position of the last result to retrieve. Zero mean
 * this argument will be ignored
 * @param order
 * @param projectionList
 * @param criterion Find conditions
 * @return List of entity that match conditions
 */
@SuppressWarnings("unchecked")
protected List<T> findByCriteria(String sessionName, int firstResult, int maxResult, Order order,
        ProjectionList projectionList, Criterion... criterion) {
    try {
        Criteria crit = getSession(sessionName).createCriteria(getPersistentClass());
        if (criterion != null) {
            for (Criterion c : criterion) {
                crit.add(c);
            }
        }
        if (firstResult >= 0) {
            crit.setFirstResult(firstResult);
        }
        if (maxResult >= 0) {
            crit.setMaxResults(maxResult);
        }
        if (order != null) {
            crit.addOrder(order);
        }
        if (projectionList != null) {
            crit.setProjection(projectionList);
        }
        List<T> result = crit.list();
        return result;
    } catch (RuntimeException e) {
        throw e;
    } finally {
        this.releaseResource();
    }
}

From source file:com.vmware.bdd.dal.DAL.java

License:Open Source License

/**
 * Fetch and return a list of objects of the given class which
 * is a subset of the all the records that satisfy the given
 * criteria. The subset is specified by parameter firstResult
 * and maxResults.//from w w w .j av  a  2s. c om
 *
 * @param aClass -- the class of the object.
 * @param order -- the result set order.
 * @param firstResult -- the offset in the query result where the returned list starts.
 * @param maxResults -- the maximum records returned.
 * @param queryCriteria -- the criteria to filter the results.
 * @return The list of matching objects.
 * @throws HibernateException -- on Hibernate errors.
 */
private static <T> List<T> findByCriteria(final Class<T> aClass, final Order[] order, final Integer firstResult,
        final Integer maxResults, final Criterion... queryCriteria) {
    return autoTransactionDo(new Saveable<List<T>>() {
        @SuppressWarnings("unchecked")
        public List<T> body() {
            Criteria criteria = getSession().createCriteria(aClass);
            for (Criterion c : queryCriteria) {
                criteria.add(c);
            }

            if (order != null) {
                for (Order o : order) {
                    criteria.addOrder(o);
                }
            }

            if (firstResult != null) {
                criteria.setFirstResult(firstResult);
            }

            if (maxResults != null) {
                criteria.setMaxResults(maxResults);
                criteria.setFetchSize(maxResults); // just a hint
            }

            return criteria.list();
        }
    });
}

From source file:com.vmware.bdd.dal.impl.BaseDAO.java

License:Open Source License

/**
 * Fetch and return a list of objects of the given class which is a subset of
 * the all the records that satisfy the given criteria. The subset is
 * specified by parameter firstResult and maxResults.
 *
 * @param order//  w w w  . jav  a  2s . c o m
 *           -- the result set order.
 * @param firstResult
 *           -- the offset in the query result where the returned list
 *           starts.
 * @param maxResults
 *           -- the maximum records returned.
 * @param queryCriteria
 *           -- the criteria to filter the results.
 * @return The list of matching objects.
 * @throws HibernateException
 *            -- on Hibernate errors.
 */
@SuppressWarnings("unchecked")
protected List<T> findByCriteria(final Order[] order, final Integer firstResult, final Integer maxResults,
        final Criterion... queryCriteria) {
    Criteria criteria = sessionFactory.getCurrentSession().createCriteria(type);
    for (Criterion c : queryCriteria) {
        criteria.add(c);
    }

    if (order != null) {
        for (Order o : order) {
            criteria.addOrder(o);
        }
    }

    if (firstResult != null) {
        criteria.setFirstResult(firstResult);
    }

    if (maxResults != null) {
        criteria.setMaxResults(maxResults);
        criteria.setFetchSize(maxResults); // just a hint
    }

    return criteria.list();
}

From source file:com.wavemaker.runtime.data.task.AbstractReadTask.java

License:Open Source License

protected void applyPaging(PagingOptions options, Criteria c) {

    if (options == null) {
        return;/*from www .ja v a2  s  .  com*/
    }

    Long firstResult = options.getFirstResult();
    if (firstResult != null) {
        c.setFirstResult(firstResult.intValue());
    }
    Long maxResults = options.getMaxResults();
    if (maxResults != null) {
        c.setMaxResults(maxResults.intValue());
    }

    // this options instance can be re-used for the next page
    options.nextPage();
}

From source file:com.websystique.springmvc.dao.UsersDaoIml.java

@SuppressWarnings("unchecked")
public Collection<Users> findAllUsers(Integer offset, Integer maxResults) {

    Criteria criteria = createEntityCriteria().addOrder(Order.asc("firstName"));
    criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);//To avoid duplicates.
    criteria.setFirstResult(offset != null ? offset : 0).setMaxResults(maxResults != null ? maxResults : 20);
    List<Users> users = (List<Users>) criteria.list();

    return users;

    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

From source file:com.wonders.bud.freshmommy.service.content.dao.impl.ContentDaoImpl.java

License:Open Source License

@Override
public Page<ContentPO> findByPageCustom(Page<ContentPO> page, String orQuery) {
    Criteria c = createCriteria();
    if (page.getParam() != null) {
        c = page.getParam().andCriteria(c);
    }//from   www  .  j  a v a  2s .c  o  m
    if (orQuery != null) {
        Criterion cTitle = Restrictions.like("title", "%" + orQuery + "%");
        Criterion cShortTitle = Restrictions.like("shortTitle", "%" + orQuery + "%");
        LogicalExpression orExp1 = Restrictions.or(cTitle, cShortTitle);
        Criterion cTxt = Restrictions.like("txt", "%" + orQuery + "%");
        LogicalExpression orExp = Restrictions.or(orExp1, cTxt);
        c.add(orExp);
    }

    c.setProjection(null);

    // ??
    int totalCount = ((Long) c.setProjection(Projections.rowCount()).uniqueResult()).intValue();

    c.setProjection(null);
    c.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);

    // page
    if (page.getStart() > -1) {
        c.setFirstResult(page.getStart());
    }
    if (page.getPagesize() > -1) {
        c.setMaxResults(page.getPagesize());
    }
    List<ContentPO> result = c.list();
    getSession().clear();
    page.setResult(result);
    page.setTotal(totalCount);
    return page;
}

From source file:com.xbwl.common.orm.hibernate.HibernateDao.java

License:Apache License

/**
 * Criteria,./* w  ww .ja  va2 s .co m*/
 */
protected Criteria setPageParameter(final Criteria c, final Page<T> page) {
    //hibernatefirstResult0
    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:com.xxl.report.service.ReportService.java

public PageList getReportModuleList(Integer systemID, ReportModuleVO searchVO, Integer firstResult,
        Integer fetchSize) throws CommonException {
    try {//from w  ww.  jav  a2s .c o m
        hibernateSession = HibernateUtil.currentSession();
        Criteria criteria = hibernateSession.createCriteria(ReportModule.class);
        int size = fetchSize.intValue();
        int system = systemID.intValue();
        if (this.isSystemModule(system)) {
            system = 0;// ???????????????????????????????????????????????????
        }
        if (searchVO != null) {
            if (system == 0 && searchVO.getModuleID() != null) {
                system = searchVO.getModuleID().intValue();
            }
            if (searchVO.getName() != null) {
                criteria.add(Expression.like("name", "%" + searchVO.getName() + "%"));
            }
            if (searchVO.getReport() != null) {
                criteria.add(Expression.like("report", "%" + searchVO.getReport() + "%"));
            }
            if (searchVO.getRemark() != null) {
                criteria.add(Expression.like("remark", "%" + searchVO.getRemark() + "%"));
            }
        }
        if (system != 0) {
            //            List modules = SemAppUtils.getSubmodules(system,
            //                  hibernateSession);
            //            criteria.add(Expression.in("module", getSubmodules(system,
            //                  hibernateSession)));
            ItModule module = new ItModule();
            module.setId(new Integer(system));
            criteria.add(Expression.eq("module", module));

        }
        Integer rowCount = (Integer) criteria.setProjection(Projections.rowCount()).uniqueResult();
        criteria.setProjection(null);
        if (size > 0) {
            criteria.setFirstResult(firstResult.intValue());
            criteria.setMaxResults(size);
        }
        criteria.addOrder(Order.desc("module"));
        List list = new ArrayList();
        Iterator iter = criteria.list().iterator();
        while (iter.hasNext()) {
            ReportModule reportModule = (ReportModule) iter.next();
            ReportModuleVO vo = (ReportModuleVO) reportModule.toVO();
            list.add(vo);
        }
        logger.debug("report admin EJB rowCount=" + rowCount);
        PageList pageList = new PageList();
        pageList.setResults(rowCount.intValue());
        pageList.setItems(list);
        return pageList;
    } catch (HibernateException ee) {
        logger.error(ee);
        throw new CommonException("??" + ee.getMessage());

    } finally {
        try {
            HibernateUtil.closeSession();
        } catch (HibernateException e) {
        }
    }
}