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.webbudget.domain.model.repository.financial.FixedMovementRepository.java

License:Open Source License

/**
 *
 * @param filter//from  ww w  .ja  va2  s  .co  m
 * @param pageRequest
 * @return
 */
@Override
public Page<FixedMovement> listByFilter(String filter, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    final List<Criterion> criterions = new ArrayList<>();

    // filtramos
    if (filter != null && !filter.isEmpty()) {

        criterions.add(Restrictions.ilike("description", "%" + filter + "%"));
        criterions.add(Restrictions.ilike("identification", "%" + filter + "%"));

        // se conseguir castar para bigdecimal trata como um filtro
        try {
            criterions.add(Restrictions.eq("value", new BigDecimal(filter)));
        } catch (NumberFormatException ex) {
        }
    }

    criteria.add(Restrictions.or(criterions.toArray(new Criterion[] {})));

    // 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.financial.LaunchRepository.java

License:Open Source License

/**
 * //from   www .  j  av a2s .  c  o  m
 * @param fixedMovement
 * @param pageRequest
 * @return 
 */
@Override
public Page<Launch> listByFixedMovement(FixedMovement fixedMovement, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    criteria.createAlias("fixedMovement", "fm");
    criteria.add(Restrictions.eq("fm.id", fixedMovement.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.financial.MovementRepository.java

License:Open Source License

/**
 *
 * @param filter//w  ww  .java2s.  c o  m
 * @param pageRequest
 * @return
 */
@Override
public Page<Movement> listByFilter(MovementFilter filter, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    final List<Criterion> criterions = new ArrayList<>();

    criteria.createAlias("contact", "co", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("apportionments", "ap");
    criteria.createAlias("ap.movementClass", "mc");
    criteria.createAlias("ap.costCenter", "cc");
    criteria.createAlias("financialPeriod", "fp");

    // montramos os criterios de filtragem geral
    if (filter.hasCriteria()) {
        criterions.add(Restrictions.eq("code", filter.getCriteria()));
        criterions.add(Restrictions.ilike("description", "%" + filter.getCriteria() + "%"));
        criterions.add(Restrictions.ilike("mc.name", "%" + filter.getCriteria() + "%"));
        criterions.add(Restrictions.ilike("cc.name", "%" + filter.getCriteria() + "%"));
        criterions.add(Restrictions.ilike("co.name", "%" + filter.getCriteria() + "%"));

        // se conseguir castar para bigdecimal trata como um filtro
        try {
            criterions.add(Restrictions.eq("value", filter.criteriaToBigDecimal()));
        } catch (ParseException ex) {
        }
    }

    criteria.add(Restrictions.or(criterions.toArray(new Criterion[] {})));
    criteria.add(Restrictions.and(filter.getCustomFilters()));

    // 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.DISTINCT_ROOT_ENTITY);

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

    // aplica o multisort dos campos
    if (pageRequest.isMultiSort()) {
        pageRequest.getMultiSortFields().stream().forEach(field -> {
            if (field.getDirection() == SortDirection.ASC) {
                criteria.addOrder(Order.asc(field.getSortField()));
            } else if (field.getDirection() == SortDirection.DESC) {
                criteria.addOrder(Order.desc(field.getSortField()));
            }
        });
    } else if (pageRequest.getSortDirection() == SortDirection.ASC) {
        criteria.addOrder(Order.asc(pageRequest.getSortField()));
    } else if (pageRequest.getSortDirection() == 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.logbook.RefuelingRepository.java

License:Open Source License

/**
 *
 * @param filter//  w  w  w.  j a v  a  2s .co  m
 * @param pageRequest
 * @return
 */
@Override
public Page<Refueling> listLazily(String filter, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (StringUtils.isNotBlank(filter)) {
        // TODO colocar os filtros aqui
    }

    // 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.miscellany.FinancialPeriodRepository.java

License:Open Source License

/**
 * //from w  w  w .j a  v a 2  s.co  m
 * @param isClosed
 * @param pageRequest
 * @return 
 */
@Override
public Page<FinancialPeriod> listByStatusLazily(Boolean isClosed, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

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

    // 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.miscellany.FinancialPeriodRepository.java

License:Open Source License

/**
 * //from   ww  w. j  ava 2 s  .c o m
 * @return 
 */
@Override
public List<FinancialPeriod> listLastSixClosed() {

    final Criteria criteria = this.createCriteria();

    criteria.add(Restrictions.eq("closed", true));
    criteria.addOrder(Order.desc("id"));
    criteria.setMaxResults(6);

    criteria.addOrder(Order.desc("inclusion"));

    return criteria.list();
}

From source file:br.com.webbudget.domain.model.repository.tools.MessageRepository.java

License:Open Source License

/**
 * //from  w w  w  . j  a  va2s  .  c  o  m
 * @param sender
 * @param filter
 * @param pageRequest
 * @return 
 */
@Override
public Page<Message> listSent(User sender, String filter, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (filter != null) {
        criteria.add(Restrictions.or(Restrictions.ilike("title", "%" + filter + "%"),
                Restrictions.ilike("content", "%" + filter + "%")));
    }

    criteria.add(Restrictions.eq("deleted", false));
    criteria.add(Restrictions.eq("sender", sender));

    // 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.tools.UserMessageRepository.java

License:Open Source License

/**
 * /*from w ww .  j  a va2s  .  c  o  m*/
 * @param recipient
 * @param filter
 * @param pageRequest
 * @return 
 */
@Override
public Page<UserMessage> listReceived(User recipient, String filter, PageRequest pageRequest) {

    final Criteria criteria = this.createCriteria();

    if (filter != null) {
        criteria.createAlias("message", "ms");
        criteria.add(Restrictions.or(Restrictions.ilike("ms.title", "%" + filter + "%"),
                Restrictions.ilike("ms.content", "%" + filter + "%")));
    }

    criteria.add(Restrictions.eq("deleted", false));
    criteria.add(Restrictions.eq("recipient", recipient));

    // 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.edu.unipampa.gerenciadorconcurso.dao.DAO.java

public static Object buscarObjeto(HashMap<String, Object> filtros, Class<?> classe) {
    Object objeto = null;/* w w w . ja va2 s .c  o m*/
    Criteria criteria = getCriteria(classe);
    for (Map.Entry<String, Object> entry : filtros.entrySet()) {
        String campo = entry.getKey();
        Object valor = entry.getValue();
        criteria.add(Restrictions.eq(campo, valor));
    }
    criteria.setMaxResults(1);//no maximo 1 resultado
    return getObject(criteria);
}

From source file:br.gov.jfrj.siga.ex.bl.ExBL.java

License:Open Source License

public void numerarTudo(int aPartirDe) {
    List<ExDocumento> list = new ArrayList<ExDocumento>();

    final Criteria countCrit = dao().getSessao().createCriteria(ExDocumento.class)
            .add(Restrictions.gt("idDoc", new Long(aPartirDe)));
    countCrit.setProjection(Projections.rowCount());
    Integer totalDocs = ((Long) countCrit.uniqueResult()).intValue();

    final Criteria crit = dao().getSessao().createCriteria(ExDocumento.class)
            .add(Restrictions.gt("idDoc", new Long(aPartirDe)));
    crit.setMaxResults(60);
    crit.addOrder(Order.asc("idDoc"));

    int index = 0;

    do {//from   w  ww. ja  v a2  s . c  om
        long inicio = System.currentTimeMillis();
        //System.gc();
        iniciarAlteracao();
        crit.setFirstResult(index);
        list = crit.list();
        for (ExDocumento doc : list) {
            index++;
            try {
                for (ExMovimentacao m : doc.getExMovimentacaoSet()) {
                    m.setNumPaginas(m.getContarNumeroDePaginas());
                    dao().gravar(m);
                }
            } catch (Throwable e) {
                System.out.println("Erro ao marcar o doc " + doc);
                e.printStackTrace();
            }
            if (index % 50 == 0) {
                //               System.gc();
            }
            System.out.print(doc.getIdDoc() + " ok - ");
        }
        ExDao.commitTransacao();
        dao().getSessao().clear();
        long duracao = System.currentTimeMillis() - inicio;
        System.out.println();
        System.out.println(new SimpleDateFormat("HH:mm:ss").format(new Date()) + " " + String.valueOf(index)
                + " numerados de " + totalDocs);
    } while (list.size() > 0);

    //System.gc();
}