Example usage for javax.persistence TypedQuery setMaxResults

List of usage examples for javax.persistence TypedQuery setMaxResults

Introduction

In this page you can find the example usage for javax.persistence TypedQuery setMaxResults.

Prototype

TypedQuery<X> setMaxResults(int maxResult);

Source Link

Document

Set the maximum number of results to retrieve.

Usage

From source file:net.navasoft.madcoin.backend.model.controller.helper.impl.JPAHelper.java

/**
 * Find entities./*from w w w. ja va 2s .c  o m*/
 * 
 * @param em
 *            the em
 * @param target
 *            the target
 * @param all
 *            the all
 * @param maxResults
 *            the max results
 * @param firstResult
 *            the first result
 * @return the list
 * @since 28/08/2014, 11:20:27 PM
 */
private List<Entity> findEntities(EntityManager em, Class<Entity> target, boolean all, int maxResults,
        int firstResult) {
    CriteriaQuery<Entity> cq = em.getCriteriaBuilder().createQuery(target);
    cq.select(cq.from(target));
    TypedQuery<Entity> q = em.createQuery(cq);
    if (!all) {
        q.setMaxResults(maxResults);
        q.setFirstResult(firstResult);
    }
    return q.getResultList();
}

From source file:csns.model.academics.dao.jpa.SectionDaoImpl.java

@Override
public List<Section> searchSections(String term, int maxResults) {
    TypedQuery<Section> query = entityManager.createNamedQuery("section.search", Section.class);
    if (maxResults > 0)
        query.setMaxResults(maxResults);
    return query.setParameter("term", term).getResultList();
}

From source file:net.dontdrinkandroot.persistence.dao.GenericJpaDao.java

/**
 * Finds all entities by the given {@link CriteriaQuery} and limit the result set.
 * /*from   ww  w .  ja va 2s.  com*/
 * @param maxResults
 *            The maximum number of results to retrieve.
 */
protected <V> List<V> find(final CriteriaQuery<V> criteriaQuery, final int maxResults) {
    final TypedQuery<V> query = this.getEntityManager().createQuery(criteriaQuery);
    query.setMaxResults(maxResults);

    return query.getResultList();
}

From source file:net.dontdrinkandroot.persistence.dao.GenericJpaDao.java

/**
 * Finds all entities by the given {@link CriteriaQuery} and limit the result set.
 * /*ww  w  .  j a v a 2  s  . com*/
 * @param firstResult
 *            Position of the first result to retrive.
 * @param maxResults
 *            The maximum number of results to retrieve.
 */
protected <V> List<V> find(final CriteriaQuery<V> criteriaQuery, final int firstResult, final int maxResults) {
    final TypedQuery<V> query = this.getEntityManager().createQuery(criteriaQuery);
    query.setMaxResults(maxResults);
    query.setFirstResult(firstResult);

    return query.getResultList();
}

From source file:org.cleverbus.core.common.dao.ExternalCallDaoJpaImpl.java

@Override
public List<ExternalCall> findProcessingExternalCalls(int interval) {
    final Date startProcessLimit = DateUtils.addSeconds(new Date(), -interval);

    String jSql = "SELECT c " + "FROM " + ExternalCall.class.getName() + " c " + "WHERE c.state = '"
            + ExternalCallStateEnum.PROCESSING + "'" + "     AND c.lastUpdateTimestamp < :time";

    TypedQuery<ExternalCall> q = em.createQuery(jSql, ExternalCall.class);
    q.setParameter("time", new Timestamp(startProcessLimit.getTime()));
    q.setMaxResults(MAX_MESSAGES_IN_ONE_QUERY);
    return q.getResultList();
}

From source file:br.eti.danielcamargo.backend.hsnpts.core.business.MicrocicloService.java

/**
 * Se o microciclo existir e for incompleto, ele  retornado.
 * <br />/*from   w  w  w  . j av  a  2  s.c  o  m*/
 * Se o microciclo no existir ou estiver completo, cria um novo.
 *
 * @param alunoId
 * @return
 * @throws
 * br.eti.danielcamargo.backend.hsnpts.core.business.exceptions.ProgramaFinalizadoException
 */
@Transactional
public Microciclo resolverMicrociclo(Long alunoId) throws ProgramaFinalizadoException {

    StringBuilder hql = new StringBuilder();

    hql.append("SELECT ");
    hql.append(" p ");
    hql.append("FROM ");
    hql.append("  Programa p ");
    hql.append("WHERE ");
    hql.append("  p.aluno.id = :alunoId ");
    hql.append("  AND p.dataTermino IS NULL ");

    TypedQuery<Programa> queryPrograma = em.createQuery(hql.toString(), Programa.class);
    queryPrograma.setParameter("alunoId", alunoId);
    Programa programa = queryPrograma.getSingleResult();

    hql.append("SELECT ");
    hql.append(" m ");
    hql.append("FROM ");
    hql.append(" Microciclo m ");
    hql.append("WHERE ");
    hql.append(" m.programa = :programa ");
    hql.append("ORDER BY ");
    hql.append(" m.numero DESC ");

    TypedQuery<Microciclo> queryMicrociclo = em.createQuery(hql.toString(), Microciclo.class);
    queryMicrociclo.setParameter("programa", programa);
    queryMicrociclo.setMaxResults(1);

    List<Microciclo> result = queryMicrociclo.getResultList();

    HsnPersonal hsnPersonal = HsnPersonalUtils.getInstance();
    ObjetivoPrograma objetivoPrograma = programa.getObjetivoPrograma();
    FrequenciaSemanal frequenciaSemanal = programa.getFrequenciaSemanal();

    if (result.isEmpty()) {
        //criar primeiro microciclo
        Microciclo microciclo = hsnPersonal.buscarMicrociclo(objetivoPrograma, frequenciaSemanal, 1);
        microciclo.setPrograma(programa);
        save(microciclo);
        return microciclo;
    } else {
        //verificar se o programa esta finalizado
        Microciclo microciclo = result.get(0);
        if (microciclo.getDataTermino() != null) {
            if (microciclo.getNumero().equals(24)) {
                throw new ProgramaFinalizadoException();
            }
            microciclo = hsnPersonal.buscarMicrociclo(objetivoPrograma, frequenciaSemanal, 1);
            microciclo.setPrograma(programa);
            save(microciclo);
            return microciclo;
        }
        return microciclo;
    }
}

From source file:net.groupbuy.dao.impl.ArticleCategoryDaoImpl.java

public List<ArticleCategory> findRoots(Integer count) {
    String jpql = "select articleCategory from ArticleCategory articleCategory where articleCategory.parent is null order by articleCategory.order asc";
    TypedQuery<ArticleCategory> query = entityManager.createQuery(jpql, ArticleCategory.class)
            .setFlushMode(FlushModeType.COMMIT);
    if (count != null) {
        query.setMaxResults(count);
    }// www. ja  v a2s  .c om
    return query.getResultList();
}

From source file:example.springdata.jpa.showcase.before.CustomerServiceImpl.java

@Override
public List<Customer> findByLastname(String lastname, int page, int pageSize) {

    TypedQuery<Customer> query = em.createQuery("select c from Customer c where c.lastname = ?1",
            Customer.class);

    query.setParameter(1, lastname);//w w w  . j  ava 2  s . c om
    query.setFirstResult(page * pageSize);
    query.setMaxResults(pageSize);

    return query.getResultList();
}

From source file:eu.domibus.common.dao.MessageLogDao.java

public List<MessageLogEntry> findPaged(int from, int max, String column, boolean asc,
        HashMap<String, Object> filters) {

    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<MessageLogEntry> cq = cb.createQuery(MessageLogEntry.class);
    Root<MessageLogEntry> mle = cq.from(MessageLogEntry.class);
    cq.select(mle);/* w ww  . j  a  v a2  s . com*/
    List<Predicate> predicates = new ArrayList<Predicate>();
    for (Map.Entry<String, Object> filter : filters.entrySet()) {
        if (filter.getValue() != null) {
            if (filter.getValue() instanceof String) {
                if (!filter.getValue().toString().isEmpty()) {
                    switch (filter.getKey().toString()) {
                    case "receivedFrom":
                        predicates.add(cb.greaterThanOrEqualTo(mle.<Date>get("received"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    case "receivedTo":
                        predicates.add(cb.lessThanOrEqualTo(mle.<Date>get("received"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    default:
                        predicates.add(cb.like(mle.<String>get(filter.getKey()), (String) filter.getValue()));
                        break;
                    }
                }
            } else {
                predicates.add(cb.equal(mle.<String>get(filter.getKey()), filter.getValue()));
            }
        }
    }
    cq.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    if (column != null) {
        if (asc) {
            cq.orderBy(cb.asc(mle.get(column)));
        } else {
            cq.orderBy(cb.desc(mle.get(column)));
        }

    }
    final TypedQuery<MessageLogEntry> query = this.em.createQuery(cq);
    query.setFirstResult(from);
    query.setMaxResults(max);
    return query.getResultList();
}

From source file:com.epam.ipodromproject.repository.jpa.JPABetRepository.java

@Transactional
@Override/* w ww. j a  va 2s  . co  m*/
public List<Bet> getActiveBetsMadeByUser(String username, int startingResult, int resultsCount) {
    TypedQuery<Bet> query = entityManager.createNamedQuery("Bet.findByUsernameByBetResult", Bet.class);
    query.setParameter("username", username);
    query.setParameter("betResult", BetResult.NOT_CONSIDERED);
    return query.setMaxResults(resultsCount).setFirstResult(startingResult).getResultList();
}