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:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
@Nullable/*w ww . j a va  2  s .c  o  m*/
public Message findPostponedMessage(int interval) {
    // find message that was lastly processed before specified interval
    Date lastUpdateLimit = DateUtils.addSeconds(new Date(), -interval);

    String jSql = "SELECT m " + "FROM " + Message.class.getName() + " m " + "WHERE m.state = '"
            + MsgStateEnum.POSTPONED + "'" + "     AND m.lastUpdateTimestamp < :lastTime"
            + " ORDER BY m.msgTimestamp";

    TypedQuery<Message> q = em.createQuery(jSql, Message.class);
    q.setParameter("lastTime", new Timestamp(lastUpdateLimit.getTime()));
    q.setMaxResults(1);
    List<Message> messages = q.getResultList();

    if (messages.isEmpty()) {
        return null;
    } else {
        return messages.get(0);
    }
}

From source file:com.music.dao.PieceDao.java

public List<Piece> getUserPieces(Long userId, int page, int pageSize) {
    TypedQuery<Piece> query = getEntityManager().createQuery(
            "SELECT pe.piece FROM PieceEvaluation pe WHERE pe.user.id=:userId AND pe.positive = true ORDER BY pe.dateTime DESC",
            Piece.class);
    query.setParameter("userId", userId);
    query.setFirstResult(page * pageSize);
    query.setMaxResults(pageSize);

    return query.getResultList();
}

From source file:eu.ggnet.dwoss.report.ReportAgentBean.java

@Override
public List<ReportLine> find(SearchParameter search, int firstResult, int maxResults) {
    StringBuilder sb = new StringBuilder("Select l from ReportLine l");
    if (!StringUtils.isBlank(search.getRefurbishId()))
        sb.append(" where l.refurbishId = :refurbishId");
    L.debug("Using created SearchQuery:{}", sb);
    TypedQuery<ReportLine> q = reportEm.createQuery(sb.toString(), ReportLine.class);
    if (!StringUtils.isBlank(search.getRefurbishId()))
        q.setParameter("refurbishId", search.getRefurbishId().trim());
    q.setFirstResult(firstResult);/*from  w  ww . j  a v  a  2  s  . c om*/
    q.setMaxResults(maxResults);
    return q.getResultList();
}

From source file:no.dusken.momus.service.ArticleService.java

public List<Article> searchForArticles(ArticleSearchParams params) {
    long start = System.currentTimeMillis();

    ArticleQueryBuilder builder = new ArticleQueryBuilder(params);
    String queryText = builder.getFullQuery();
    Map<String, Object> queryParams = builder.getQueryParams();

    TypedQuery<Article> query = entityManager.createQuery(queryText, Article.class);

    for (Map.Entry<String, Object> e : queryParams.entrySet()) {
        query.setParameter(e.getKey(), e.getValue());
    }//from  w w  w  .  j  a v  a2  s.c  om

    query.setMaxResults(params.getPageSize() + 1); // One extra, so searchpage can see if there is "more pages"
    query.setFirstResult(params.getStartOfPage());

    List<Article> resultList = query.getResultList();

    long end = System.currentTimeMillis();
    long timeUsed = end - start;
    logger.debug("Time spent on search: {}ms", timeUsed);
    if (timeUsed > 800) {
        logger.warn("Time spent on search high ({}ms), params were: {}", timeUsed, params);
    }

    return resultList;
}

From source file:org.verinice.persistence.Dao.java

/**
 * Configures the query and sets the maximum number and the first element 
 * which are returned from the result.//  w  ww . j ava  2s .c  o  m
 * 
 * @param query The JPA query
 * @param size The maximum number of elements which are returned from the
 *    result of the query.
 * @param firstResult The index of the first element which is returned from 
 *    the result of the query
 */
protected void configureResultSize(TypedQuery<?> query, Integer size, Integer firstResult) {
    int firstIndex;
    if (firstResult == null || firstResult < 0) {
        firstIndex = 0;
    } else {
        firstIndex = firstResult;
    }
    query.setFirstResult(firstIndex);
    int limit = getLimit(size);
    logger.debug("Result limit: " + limit);
    query.setMaxResults(limit);
}

From source file:eu.ggnet.dwoss.report.ReportAgentBean.java

@Override
public List<SimpleReportLine> findSimple(SearchParameter search, int firstResult, int maxResults) {
    StringBuilder sb = new StringBuilder("Select l from SimpleReportLine l");
    if (!StringUtils.isBlank(search.getRefurbishId()))
        sb.append(" where l.refurbishId = :refurbishId");
    L.debug("Using created SearchQuery:{}", sb);
    TypedQuery<SimpleReportLine> q = reportEm.createQuery(sb.toString(), SimpleReportLine.class);
    if (!StringUtils.isBlank(search.getRefurbishId()))
        q.setParameter("refurbishId", search.getRefurbishId().trim());
    q.setFirstResult(firstResult);/*from  w  w  w.  j  a  v a 2  s .c  o m*/
    q.setMaxResults(maxResults);
    return q.getResultList();
}

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

@Override
public List<Request> findByCriteria(Date from, Date to, String subUri, String subRequest) {
    Assert.notNull(from, "the from must not be null");
    Assert.notNull(to, "the to must not be null");

    String jSql = "SELECT r " + "         FROM " + Request.class.getName() + " r "
            + "           WHERE r.reqTimestamp >= :from " + "               AND r.reqTimestamp <= :to ";

    if (hasText(subUri)) {
        jSql += "           AND r.uri like :subUri)";
    }/*from w  w w. ja v  a 2s . c  o m*/
    if (hasText(subRequest)) {
        jSql += "           AND r.request like :subRequest)";
    }

    jSql += "           ORDER BY r.reqTimestamp";

    TypedQuery<Request> q = em.createQuery(jSql, Request.class);
    q.setParameter("from", new Timestamp(from.getTime()));
    q.setParameter("to", new Timestamp(to.getTime()));
    if (hasText(subUri)) {
        q.setParameter("subUri", "%" + subUri + "%");
    }
    if (hasText(subRequest)) {
        q.setParameter("subRequest", "%" + subRequest + "%");
    }
    q.setMaxResults(MAX_REQUESTS_IN_ONE_QUERY);

    return q.getResultList();
}

From source file:org.mitre.oauth2.repository.impl.JpaOAuth2TokenRepository.java

@Override
public Set<OAuth2AccessTokenEntity> getAllExpiredAccessTokens() {
    TypedQuery<OAuth2AccessTokenEntity> query = manager
            .createNamedQuery(OAuth2AccessTokenEntity.QUERY_EXPIRED_BY_DATE, OAuth2AccessTokenEntity.class);
    query.setParameter(OAuth2AccessTokenEntity.PARAM_DATE, new Date());
    query.setMaxResults(MAXEXPIREDRESULTS);
    return new LinkedHashSet<>(query.getResultList());
}

From source file:org.businessmanager.dao.GenericDaoImpl.java

@Override
public List<T> findAll(SingularAttribute<T, ?> orderAttribute, boolean orderAsc, int firstResult,
        int maxResults, Map<SingularAttribute<T, ?>, Object> filterAttributes, boolean enableLikeSearch) {
    CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass());
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());

    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);

    List<Predicate> predicateList = createFilterList(filterAttributes, enableLikeSearch, queryBuilder,
            rootQuery);/*from  www  . j a  v  a 2s.  com*/
    select.where(predicateList.toArray(new Predicate[0]));

    if (orderAsc) {
        criteriaQuery.orderBy(queryBuilder.asc(rootQuery.get(orderAttribute)));
    } else {
        criteriaQuery.orderBy(queryBuilder.desc(rootQuery.get(orderAttribute)));
    }

    TypedQuery<T> typedQuery = entityManager.createQuery(criteriaQuery);

    if (firstResult != -1) {
        typedQuery.setFirstResult(firstResult);
    }
    if (maxResults != -1) {
        typedQuery.setMaxResults(maxResults);
    }

    return typedQuery.getResultList();
}

From source file:org.mitre.oauth2.repository.impl.JpaOAuth2TokenRepository.java

@Override
public Set<OAuth2RefreshTokenEntity> getAllExpiredRefreshTokens() {
    TypedQuery<OAuth2RefreshTokenEntity> query = manager
            .createNamedQuery(OAuth2RefreshTokenEntity.QUERY_EXPIRED_BY_DATE, OAuth2RefreshTokenEntity.class);
    query.setParameter(OAuth2RefreshTokenEntity.PARAM_DATE, new Date());
    query.setMaxResults(MAXEXPIREDRESULTS);
    return new LinkedHashSet<>(query.getResultList());
}