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:br.com.prototipo.dao.TurmaDao.java

public Turma getTurma(String codigo) {
    if (sessao.isConnected()) {
        sessao.close();/*from  w w w . j  a v  a2s. c o m*/
    }
    sessao = HibernateUtil.getSessionFactory().openSession();

    Criteria cri = sessao.createCriteria(Turma.class);
    Criterion _cod = Restrictions.like("codigo", codigo, MatchMode.EXACT);
    cri.add(_cod);
    cri.setMaxResults(1);
    Object result = cri.uniqueResult();
    Turma turma = new Turma();
    if (result != null) {
        turma = (Turma) result;
    }
    return turma;
}

From source file:br.com.tesouro.controll.facade.AbstractFacade.java

public List<T> findRange(int[] range) {
    Criteria q = getSession().createCriteria(entityClass);
    q.setMaxResults(range[1] - range[0] + 1);
    q.setFirstResult(range[0]);//from w  w w.  ja va2s .c  o m
    return q.list();
}

From source file:br.com.thiaguten.persistence.spi.provider.hibernate.HibernatePersistenceProvider.java

License:Apache License

private Criteria criteriaRange(Criteria criteria, int firstResult, int maxResults) {
    if (criteria != null) {
        if (maxResults >= 0) {
            criteria.setMaxResults(maxResults);
        }/*  w  w  w .j av a  2  s.co m*/
        if (firstResult >= 0) {
            criteria.setFirstResult(firstResult);
        }
    }
    return criteria;
}

From source file:br.com.webbudget.domain.model.repository.entries.CardInvoiceRepository.java

License:Open Source License

/**
 *
 * @param card/*from w ww  .  j a v  a 2  s  . co  m*/
 * @param pageRequest
 * @return
 */
@Override
public Page<CardInvoice> listByCard(Card card, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    // filtra
    if (card != null) {
        criteria.createAlias("card", "ca");
        criteria.add(Restrictions.eq("ca.id", card.getId()));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.entries.CardRepository.java

License:Open Source License

/**
 * /*from w  w  w . j av  a2  s .co m*/
 * @param isBlocked
 * @param pageRequest
 * @return 
 */
@Override
public Page<Card> listByStatus(Boolean isBlocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (isBlocked != null) {
        criteria.add(Restrictions.eq("blocked", isBlocked));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.entries.ContactRepository.java

License:Open Source License

/**
 *
 * @param filter//from  w  w  w . j  ava 2  s.c o m
 * @param blocked
 * @param pageRequest
 * @return
 */
@Override
public Page<Contact> listLazilyByFilter(String filter, Boolean blocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (filter != null) {
        criteria.add(Restrictions.or(Restrictions.ilike("name", "%" + filter + "%"),
                Restrictions.ilike("email", "%" + filter + "%"), Restrictions.eq("document", filter),
                Restrictions.ilike("city", "%" + filter + "%")));
    }

    if (blocked != null) {
        criteria.add(Restrictions.eq("blocked", blocked));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.entries.CostCenterRepository.java

License:Open Source License

/**
 * /*from w w  w.j  a  v  a  2s .  c  o  m*/
 * @param isBlocked
 * @param pageRequest
 * @return 
 */
@Override
public Page<CostCenter> listLazilyByStatus(Boolean isBlocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (isBlocked != null) {
        criteria.add(Restrictions.eq("blocked", isBlocked));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.entries.MovementClassRepository.java

License:Open Source License

/**
 * //www.  j  a va2  s  .  c  om
 * @param isBlocked
 * @param pageRequest
 * @return 
 */
@Override
public Page<MovementClass> listLazilyByStatus(Boolean isBlocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (isBlocked != null) {
        criteria.add(Restrictions.eq("blocked", isBlocked));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.entries.VehicleRepository.java

License:Open Source License

/**
 *
 * @param isBlocked//from   w w  w . jav  a 2 s  .  c om
 * @param pageRequest
 * @return
 */
@Override
public Page<Vehicle> listLazilyByStatus(Boolean isBlocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (isBlocked != null) {
        criteria.add(Restrictions.eq("blocked", isBlocked));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}

From source file:br.com.webbudget.domain.model.repository.entries.WalletRepository.java

License:Open Source License

/**
 * //  w ww  .  jav a  2  s .c  o  m
 * @param isBlocked
 * @param pageRequest
 * @return 
 */
@Override
public Page<Wallet> listLazilyByStatus(Boolean isBlocked, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (isBlocked != null) {
        criteria.add(Restrictions.eq("blocked", isBlocked));
    }

    // projetamos para pegar o total de paginas possiveis
    criteria.setProjection(Projections.count("id"));

    final Long totalRows = (Long) criteria.uniqueResult();

    // limpamos a projection para que a criteria seja reusada
    criteria.setProjection(null);
    criteria.setResultTransformer(Criteria.ROOT_ENTITY);

    // paginamos
    criteria.setFirstResult(pageRequest.getFirstResult());
    criteria.setMaxResults(pageRequest.getPageSize());

    if (pageRequest.getSortDirection() == PageRequest.SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == PageRequest.SortDirection.DESC) {
        criteria.addOrder(Order.desc(pageRequest.getSortField()));
    }

    // montamos o resultado paginado
    return new Page<>(criteria.list(), totalRows);
}