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.inventory.system.dao.StudentRegistrationDAOImpl.java

@Override
public List<StudentRegistration> fetchStudentsByDateRange(Date fromDate, Date toDate) {
    Session session = HibernateBDUtil.getSessionFactory().openSession();
    List<StudentRegistration> list;

    Criteria criteria = session.createCriteria(StudentRegistration.class);
    criteria.add(Restrictions.between("registeredDate", fromDate, toDate));
    criteria.setFirstResult(0);
    criteria.setMaxResults(500);//w w  w .j  av a 2 s.  co  m
    criteria.addOrder(Order.desc("id"));
    list = criteria.list();

    return list;
}

From source file:com.inventory.system.dao.StudentRegistrationDAOImpl.java

@Override
public List<StudentRegistration> fetchRegisteredStudents() {
    Session session = HibernateBDUtil.getSessionFactory().openSession();
    List<StudentRegistration> list;

    Criteria criteria = session.createCriteria(StudentRegistration.class);
    criteria.setFirstResult(0);
    criteria.setMaxResults(500);/*www  . ja  v a 2  s.co  m*/
    criteria.addOrder(Order.desc("id"));
    list = criteria.list();

    return list;
}

From source file:com.iqtb.validacion.dao.DaoSociosComerciales.java

@Override
public List<SociosComerciales> listaSociosCriteria(Integer idEmpresa, Integer inicio, Integer fin,
        String sortField, SortOrder sortOrder, Map<String, String> filters) throws Exception {
    List<SociosComerciales> lista = null;
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction tx = session.beginTransaction();

    Criteria cr = session.createCriteria(SociosComerciales.class);
    cr.add(Restrictions.eq("idEmpresa", idEmpresa));

    if (sortField != null && !sortField.isEmpty()) {
        if (sortOrder.equals(SortOrder.ASCENDING)) {
            cr.addOrder(Order.asc(sortField));
        } else {/*from w  w w  .  j a  v a 2  s.c om*/
            cr.addOrder(Order.desc(sortField));
        }
    }
    if (!filters.isEmpty()) {
        Iterator it = filters.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            cr.add(Restrictions.like(key, "" + filters.get(key) + "%"));
        }
    }
    cr.setFirstResult(inicio);
    cr.setMaxResults(inicio + fin);
    lista = cr.list();

    return lista;
}

From source file:com.iqtb.validacion.managedbean.MbSociosComerciales.java

@PostConstruct
public void init() {
    socioDataList = new LazyDataModel<SociosComerciales>() {
        private static final long serialVersionUID = 1L;

        @Override//from www. java 2s. c om
        public List<SociosComerciales> load(int first, int pageSize, String sortField, SortOrder sortOrder,
                Map<String, String> filters) {
            listaSocios = new ArrayList<SociosComerciales>();
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();

            Criteria cr = session.createCriteria(SociosComerciales.class);
            Criteria crCount = session.createCriteria(SociosComerciales.class);
            crCount.setProjection(Projections.rowCount());
            cr.add(Restrictions.eq("idEmpresa", empresa.getIdEmpresa()));
            crCount.add(Restrictions.eq("idEmpresa", empresa.getIdEmpresa()));

            if (sortField != null && !sortField.isEmpty()) {
                if (sortOrder.equals(SortOrder.ASCENDING)) {
                    cr.addOrder(Order.asc(sortField));
                } else {
                    cr.addOrder(Order.desc(sortField));
                }
            } else {
                cr.addOrder(Order.desc("idSocioComercial"));
            }

            if (!filters.isEmpty()) {
                Iterator it = filters.keySet().iterator();
                while (it.hasNext()) {
                    String key = (String) it.next();
                    cr.add(Restrictions.like(key, "" + filters.get(key) + "%"));
                    crCount.add(Restrictions.like(key, "" + filters.get(key) + "%"));
                }
            }
            Integer num = ((Long) crCount.uniqueResult()).intValue();
            this.setRowCount(num);
            cr.setFirstResult(first);
            cr.setMaxResults(pageSize + first);
            listaSocios = cr.list();
            if (session.isOpen()) {
                session.clear();
                session.close();
            }
            return listaSocios;
        }
    };
}

From source file:com.isotrol.impe3.hib.query.PageSupport.java

License:Open Source License

/**
 * Page support for criteria queries./*from  w w w  .  jav a 2  s.com*/
 * @param <E> Query output type.
 * @param <D> DTO type.
 * @param count Count criteria (optional).
 * @param select Selection criteria (required).
 * @param pag Pagination (optional).
 * @param transformer Transformer (required).
 * @return
 */
public static <E, D> PageDTO<D> getPage(Criteria count, Criteria select, PaginationDTO pag,
        Function<? super E, ? extends D> transformer) {
    checkNotNull(select, "A selection query must be provided");
    final PageDTO<D> page = new PageDTO<D>();
    if (count != null) {
        count.setProjection(Projections.rowCount());
        page.setTotal(((Number) count.uniqueResult()).intValue());
    }
    if (pag != null) {
        page.setFirst(pag.getFirst());
        page.setSize(pag.getSize());
        select.setFirstResult(pag.getFirst());
        select.setMaxResults(pag.getSize());
    }
    @SuppressWarnings("unchecked")
    final List<E> list = select.list();
    page.setElements(Lists.newArrayList(Iterables.transform(list, transformer)));
    return page;
}

From source file:com.itrus.ca.common.persistence.BaseDaoImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<T> manyFind(DetachedCriteria detachedCriteria, ResultTransformer resultTransformer, int size) {
    Criteria criteria = detachedCriteria.getExecutableCriteria(getSession());
    criteria.setResultTransformer(resultTransformer);
    criteria.setFirstResult(0);
    criteria.setMaxResults(size);//w  ww  .  j a va  2 s  .com
    return criteria.list();
}

From source file:com.jacobheric.youbrew.dao.impl.ExpenseDAOImpl.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<Expense> search(ExpenseCriteria expenseCriteria) {

    Criteria c = this.getSessionFactory().getCurrentSession().createCriteria(Expense.class);

    ////from w w  w . j ava 2 s  .  c  o  m
    //Property restrictions
    if (expenseCriteria.getQuery() != null) {
        Disjunction d = Restrictions.disjunction();

        d.add(Restrictions.like("name", expenseCriteria.getQuery().trim(), MatchMode.ANYWHERE));
        d.add(Restrictions.like("tasteNotes", expenseCriteria.getQuery().trim(), MatchMode.ANYWHERE));
        d.add(Restrictions.like("brewNotes", expenseCriteria.getQuery().trim(), MatchMode.ANYWHERE));

        c.add(d);
    }

    //
    //Determine the total before limiting (useful for paging)
    c.setProjection(Projections.rowCount());
    expenseCriteria.setTotal(((Integer) c.uniqueResult()).intValue());
    c.setProjection(null);
    c.setResultTransformer(Criteria.ROOT_ENTITY);

    //
    //Start & limit grid page restrictions
    if (expenseCriteria.getStart() != null) {
        c.setFirstResult(expenseCriteria.getStart());
    }

    if (expenseCriteria.getLimit() != null) {
        c.setMaxResults(expenseCriteria.getLimit());
    }

    return c.list();
}

From source file:com.jacobheric.youbrew.dao.impl.RecipeDAOImpl.java

License:Open Source License

/**
 * @param recipeCriteria - custom criteria object
 * @return list of recipe criteria//from   ww  w . j  av  a 2  s  .co  m
 */
@SuppressWarnings("unchecked")
public List<Recipe> search(RecipeCriteria recipeCriteria) {

    Criteria c = this.getSessionFactory().getCurrentSession().createCriteria(Recipe.class);

    //
    //Property restrictions
    if (recipeCriteria.getQuery() != null) {
        Disjunction d = Restrictions.disjunction();

        d.add(Restrictions.like("name", recipeCriteria.getQuery().trim(), MatchMode.ANYWHERE));
        d.add(Restrictions.like("tasteNotes", recipeCriteria.getQuery().trim(), MatchMode.ANYWHERE));
        d.add(Restrictions.like("brewNotes", recipeCriteria.getQuery().trim(), MatchMode.ANYWHERE));

        c.add(d);
    }

    //
    //Determine the total before limiting (useful for paging)
    c.setProjection(Projections.rowCount());
    recipeCriteria.setTotal(((Integer) c.uniqueResult()).intValue());
    c.setProjection(null);
    c.setResultTransformer(Criteria.ROOT_ENTITY);

    //
    //Start & limit grid page restrictions
    if (recipeCriteria.getStart() != null) {
        c.setFirstResult(recipeCriteria.getStart());
    }

    if (recipeCriteria.getLimit() != null) {
        c.setMaxResults(recipeCriteria.getLimit());
    }

    return c.list();
}

From source file:com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.java

License:Open Source License

@Transactional(propagation = Propagation.REQUIRED)
public List getTenantUsers(ExecutionContext context, final Set tenantIds, final String name,
        final int firstResult, final int maxResults) {
    List userList = (List) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = createTenantUsersCriteria(session, tenantIds, name);
            if (firstResult >= 0) {
                criteria.setFirstResult(firstResult);
            }/*from  w  ww . ja v  a 2s .  com*/
            if (maxResults > 0) {
                criteria.setMaxResults(maxResults);
            }
            return criteria.list();
        }
    });

    List userDTOs = convertUserListToDtoList(userList);

    return userDTOs;
}

From source file:com.jaspersoft.jasperserver.api.metadata.user.service.impl.UserAuthorityServiceImpl.java

License:Open Source License

@Transactional(propagation = Propagation.REQUIRED)
public List getTenantRoles(ExecutionContext context, final Set tenantIds, final String name,
        final int firstResult, final int maxResults) {
    List roleList = (List) getHibernateTemplate().execute(new HibernateCallback() {
        public Object doInHibernate(Session session) throws HibernateException, SQLException {
            Criteria criteria = createTenantRolesCriteria(session, tenantIds, name);
            if (firstResult >= 0) {
                criteria.setFirstResult(firstResult);
            }//from   www. j av  a  2s  . c o m
            if (maxResults > 0) {
                criteria.setMaxResults(maxResults);
            }
            return criteria.list();
        }
    });
    return convertRoleListToDtoList(roleList);
}