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:abid.password.swing.dao.hibernate.AbstractHibernateDao.java

License:Apache License

@SuppressWarnings("unchecked")
public List<T> get(int first, int fetchSize) {
    Session session = sessionFactory.openSession();
    try {//from   w  w  w  . j a  v  a 2 s. co m
        Criteria criteria = session.createCriteria(persistentClass);
        criteria.setFirstResult(first);
        criteria.setMaxResults(fetchSize);
        criteria.setFetchSize(fetchSize);
        criteria.setCacheable(true);

        List<T> dataList = (List<T>) criteria.list();
        return dataList;
    } finally {
        session.close();
    }
}

From source file:abid.password.swing.dao.hibernate.AbstractHibernateDao.java

License:Apache License

@SuppressWarnings("unchecked")
public List<T> getSorted(int first, int fetchSize, String propertyName, boolean ascending) {
    Session session = sessionFactory.openSession();
    try {// w w w. j a  v  a 2 s.co m
        Criteria criteria = session.createCriteria(persistentClass);
        criteria.setFirstResult(first);
        criteria.setMaxResults(fetchSize);
        criteria.setFetchSize(fetchSize);
        criteria.setCacheable(true);

        if (ascending) {
            criteria.addOrder(Order.asc(propertyName));
        } else {
            criteria.addOrder(Order.desc(propertyName));
        }

        List<T> dataList = (List<T>) criteria.list();
        return dataList;
    } finally {
        session.close();
    }
}

From source file:abid.password.wicket.dao.AbstractHibernateDao.java

License:Apache License

@Transactional(type = TransactionType.READ_ONLY)
@SuppressWarnings("unchecked")
public List<T> get(int first, int fetchSize) {
    Criteria criteria = session.get().createCriteria(persistentClass);
    criteria.setFirstResult(first);
    criteria.setMaxResults(fetchSize);/*from w w w . j a  v a  2 s .  c  o  m*/
    criteria.setFetchSize(fetchSize);
    criteria.setCacheable(true);

    List<T> dataList = (List<T>) criteria.list();
    return dataList;
}

From source file:abid.password.wicket.dao.AbstractHibernateDao.java

License:Apache License

@Transactional(type = TransactionType.READ_ONLY)
@SuppressWarnings("unchecked")
public List<T> getSorted(int first, int fetchSize, String propertyName, boolean ascending) {
    Criteria criteria = session.get().createCriteria(persistentClass);
    criteria.setFirstResult(first);
    criteria.setMaxResults(fetchSize);/*from  w  w w.j a va 2  s  .c o m*/
    criteria.setFetchSize(fetchSize);
    criteria.setCacheable(true);

    if (ascending) {
        criteria.addOrder(Order.asc(propertyName));
    } else {
        criteria.addOrder(Order.desc(propertyName));
    }

    List<T> dataList = (List<T>) criteria.list();
    return dataList;
}

From source file:Action.StockManagement.java

public String viewStock() throws NullPointerException {
    Session s = HibernateUtil.getSession();
    Transaction t = s.beginTransaction();

    Criteria c = s.createCriteria(Current_Stock.class);
    c.setFirstResult(0);
    c.setMaxResults(10);/*from  www . j av  a  2s  .  c  o m*/
    List<Current_Stock> list = c.list();
    getReq().setAttribute("cs", list);

    /* Query q=s.createQuery(" from Current_Stock"); 
    List<Current_Stock> data=q.list();
    req.setAttribute("cs",data);
    int i=0;
    req.setAttribute("next",i);*/

    t.commit();
    return "success";
}

From source file:Action.StockManagement.java

public String viewNext() throws NullPointerException {
    Session s = HibernateUtil.getSession();
    Transaction t = s.beginTransaction();

    // int i=Integer.parseInt(req.getParameter("first"));
    Criteria c = s.createCriteria(Current_Stock.class);
    c.setFirstResult(10);
    c.setMaxResults(10);//w ww  . jav a  2  s.c  o m
    List<Current_Stock> list = c.list();
    getReq().setAttribute("cs", list);

    // req.setAttribute("first",i+2);
    /* Query q=s.createQuery(" from Current_Stock"); 
    List<Current_Stock> data=q.list();
    req.setAttribute("cs",data);
    int i=0;
    req.setAttribute("next",i);*/

    t.commit();
    return "success";
}

From source file:apm.common.core.DaoImpl.java

License:Open Source License

/**
 * /*from ww  w. j a  v a2  s. c  o m*/
 * @param page
 * @param detachedCriteria
 * @param resultTransformer
 * @return
 */
@SuppressWarnings("unchecked")
public Page<T> find(Page<T> page, DetachedCriteria detachedCriteria, ResultTransformer resultTransformer) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        page.setCount(count(detachedCriteria));
        if (page.getCount() < 1) {
            return page;
        }
    }
    Criteria criteria = detachedCriteria.getExecutableCriteria(getSession());
    criteria.setResultTransformer(resultTransformer);
    // 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:ar.com.zauber.commons.repository.SpringHibernateRepository.java

License:Apache License

/** @see Repository#find(Query) */
@SuppressWarnings("unchecked")
public final <T extends Persistible> List<T> find(final Query<T> query) {
    CriteriaSpecification criteria = getCriteriaSpecification(null, query, false);
    final SimpleQuery<T> simpleQuery = (SimpleQuery<T>) query;
    Criteria aCriteria;//ww  w  . j av a2s  . c  o m
    // TODO Esto debera ir en el metodo que hace getCriteriaSpecification
    // pero como no tiene DetachedCriteria posibilidad de setearle valores
    // para paginacin hubo que hacerlo as.
    if (simpleQuery.getPaging() != null) {
        int firstResult = (simpleQuery.getPaging().getPageNumber() - 1)
                * simpleQuery.getPaging().getResultsPerPage();
        DetachedCriteria idsDetachedCriteria = (DetachedCriteria) criteria;
        idsDetachedCriteria.setProjection(Projections.id());
        Criteria idsCriteria = idsDetachedCriteria.getExecutableCriteria(this.getSession());
        idsCriteria.setCacheable(query.getCacheable());
        idsCriteria.setFirstResult(firstResult);
        idsCriteria.setMaxResults(simpleQuery.getPaging().getResultsPerPage());
        // Se hace primero el select de los IDs y luego el de los objetos en
        // si ya que de otra manera en aquellos casos que haya objetos
        // que tienen colecciones cuenta los mismos varias veces haciendo
        // que se devuelvan menos resultados.
        List<Long> ids = idsCriteria.list();
        DetachedCriteria theCriteria = (DetachedCriteria) getCriteriaSpecification(null, query, false);
        if (ids.isEmpty()) {
            return new ArrayList<T>();
        }
        theCriteria.add(Restrictions.in("id", ids));
        aCriteria = theCriteria.getExecutableCriteria(this.getSession());

    } else {
        aCriteria = ((DetachedCriteria) criteria).getExecutableCriteria(this.getSession());
    }
    aCriteria.setCacheable(query.getCacheable());
    return aCriteria.list();
}

From source file:at.molindo.esi4j.module.hibernate.scrolling.CustomQueryScrollingSession.java

License:Apache License

private List<?> fetch(Criteria criteria, int first, int max) {
    // TODO there are better ways to scroll than setFirstResult(..)
    return criteria.setFirstResult(first).setMaxResults(max).setCacheable(false).list();
}

From source file:au.com.optus.mcas.sdp.bizservice.ott.ordertracking.batchjob.dao.impl.AbstractDaoImpl.java

License:Open Source License

/**
 * {@inheritDoc}/*from   w w w  .j a v a  2s  . c  om*/
 */
@SuppressWarnings("unchecked")
public List<E> findByCriteria(final DetachedCriteria criteria, final int firstResult, final int maxResults) {
    Assert.notNull(criteria, "Detached criterial cannot be null in call to findByCriteria()");

    if (getLogger().isDebugEnabled()) {
        getLogger().debug(String.format(
                "Finding instances of '%s' using detached criteria: %s [firstResult = %d, maxResults = %d]",
                getEntityClass(), criteria, firstResult, maxResults));
    }

    final Criteria executableCriteria = criteria.getExecutableCriteria(getHibernateSession());

    if (firstResult >= 0) {
        executableCriteria.setFirstResult(firstResult);
    }
    if (maxResults > 0) {
        executableCriteria.setMaxResults(maxResults);
    }

    return executableCriteria.list();
}