Example usage for javax.persistence Query setMaxResults

List of usage examples for javax.persistence Query setMaxResults

Introduction

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

Prototype

Query setMaxResults(int maxResult);

Source Link

Document

Set the maximum number of results to retrieve.

Usage

From source file:gumga.framework.application.GumgaCrudAndQueryNotOnlyTypedRepositoryImpl.java

@Override
public List getResultList(String hql, Map<String, Object> param, int firstresult, int maxResult) {
    javax.persistence.Query query = entityManager.createQuery(hql);
    query.setMaxResults(maxResult);
    query.setFirstResult(firstresult);/*from  w  w  w  .  ja  v  a  2s.c o m*/
    addParam(query, param);
    return query.getResultList();
}

From source file:com.sixsq.slipstream.persistence.Run.java

private static Query createNamedQuery(EntityManager em, String query) {
    Query q = em.createNamedQuery(query);
    q.setMaxResults(DEFAULT_LIMIT);
    return q;/*from w  ww. j av a  2s . c o  m*/
}

From source file:com.june.app.site.repository.jpa.VideoRepositoryImpl.java

@Override
public Video videoMain() {

    String queryString = "SELECT video FROM Video video WHERE video.useYn ='Y' ORDER BY video.regiDate desc";
    Query query = this.em.createQuery(queryString);
    query.setMaxResults(1);
    return (Video) query.getSingleResult();
}

From source file:com.june.app.site.repository.jpa.VideoRepositoryImpl.java

@SuppressWarnings("unchecked")
@Override/*from www . ja v  a2  s .c  o  m*/
public Collection<Video> videoMainList() {

    String queryString = "SELECT video FROM Video video WHERE video.useYn ='Y' ORDER BY video.regiDate desc";
    Query query = this.em.createQuery(queryString);
    query.setMaxResults(8);
    return query.getResultList();
}

From source file:com.impetus.kundera.rest.common.EntityUtils.java

/**
 * @param queryString/*from  w w w .j  a v  a  2  s  .  c  om*/
 * @param q
 */
public static void setQueryParameters(String queryString, String parameterString, Query q) {
    Map<String, String> paramsMap = new HashMap<String, String>();

    StringTokenizer st = new StringTokenizer(parameterString, "&");
    while (st.hasMoreTokens()) {
        String element = st.nextToken();
        paramsMap.put(element.substring(0, element.indexOf("=")),
                element.substring(element.indexOf("=") + 1, element.length()));
    }
    KunderaQuery kq = ((QueryImpl) q).getKunderaQuery();
    Set<Parameter<?>> parameters = kq.getParameters();
    for (String paramName : paramsMap.keySet()) {
        String value = paramsMap.get(paramName);
        if (paramName.equalsIgnoreCase("firstResult")) {
            q.setFirstResult(Integer.parseInt(value));
        } else if (paramName.equalsIgnoreCase("maxResult")) {
            q.setMaxResults(Integer.parseInt(value));
        } else if (StringUtils.isNumeric(paramName)) {
            for (Parameter param : parameters) {
                if (param.getPosition() == Integer.parseInt(paramName)) {
                    Class<?> paramClass = param.getParameterType();
                    PropertyAccessor accessor = PropertyAccessorFactory.getPropertyAccessor(paramClass);
                    Object paramValue = accessor.fromString(paramClass, value);
                    q.setParameter(Integer.parseInt(paramName), paramValue);
                    break;
                }
            }
        } else {
            for (Parameter param : parameters) {
                if (param.getName().equals(paramName)) {
                    Class<?> paramClass = param.getParameterType();
                    PropertyAccessor accessor = PropertyAccessorFactory.getPropertyAccessor(paramClass);
                    Object paramValue = accessor.fromString(paramClass, value);
                    q.setParameter(paramName, paramValue);

                    break;
                }
            }

        }
    }
}

From source file:de.berlios.jhelpdesk.dao.jpa.ArticleDAOJpa.java

public List<Article> getLastArticles(final int howMuch) {
    try {//from   w ww .ja  va 2 s  .c  om
        return (List<Article>) this.jpaTemplate.executeFind(new JpaCallback() {
            public Object doInJpa(EntityManager em) throws PersistenceException {
                Query q = em.createNamedQuery("Article.lastAdded");
                q.setMaxResults(howMuch);
                return q.getResultList();
            }
        });
    } catch (Exception ex) {
        log.error("Nie mona pobra ostatnio dodanych artykuw.", ex);
    }
    return Collections.<Article>emptyList();
}

From source file:de.berlios.jhelpdesk.dao.jpa.TicketEventDAOJpa.java

public List<TicketEvent> getLastEvents(final int howMuch) {
    return (List<TicketEvent>) this.jpaTemplate.execute(new JpaCallback() {
        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query q = em.createNamedQuery("TicketEvent.getLastFewEventsOrderByEventDateDESC");
            q.setMaxResults(howMuch);
            return q.getResultList();
        }//from  w  ww . j  a v  a 2 s .  co  m
    });
}

From source file:com.doculibre.constellio.services.FeaturedLinkServicesImpl.java

@Override
public FeaturedLink suggest(String text, RecordCollection collection) {
    FeaturedLink suggestion;//from   w w w . ja va  2  s. co  m
    EntityManager entityManager = ConstellioPersistenceContext.getCurrentEntityManager();

    String analyzedText;
    try {
        analyzedText = AnalyzerUtils.analyze(text, collection);
    } catch (Exception e) {
        analyzedText = null;
    }

    if (StringUtils.isNotBlank(analyzedText)) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT FL.id FROM FeaturedLink FL, FeaturedLink_KeywordsAnalyzed FLK, RecordCollection RC");
        sql.append(" WHERE FLK.FeaturedLink_id=FL.id AND FL.RecordCollection_id=RC.id");
        sql.append(" AND RC.id=? AND FLK.keyword=?");

        Query sqlQuery = entityManager.createNativeQuery(sql.toString());
        sqlQuery.setMaxResults(1);
        sqlQuery.setParameter(1, collection.getId());
        sqlQuery.setParameter(2, analyzedText);

        try {
            BigInteger featuredLinkId = (BigInteger) sqlQuery.getSingleResult();
            suggestion = get(featuredLinkId.longValue());
        } catch (NoResultException e) {
            suggestion = null;
        }
    } else {
        suggestion = null;
    }
    return suggestion;
}

From source file:de.berlios.jhelpdesk.dao.jpa.AnnouncementDAOJpa.java

public List<Announcement> getLastAnnouncements(final int howMuch) {
    try {/*from  ww w. j a  v  a 2s .  c o m*/
        return (List<Announcement>) this.jpaTemplate.executeFind(new JpaCallback() {
            public Object doInJpa(EntityManager em) throws PersistenceException {
                Query q = em.createNamedQuery("Announcement.allOrderByCreateDateDesc");
                q.setMaxResults(howMuch);
                return q.getResultList();
            }
        });
    } catch (Exception ex) {
        log.error("wystapil problem..", ex);
    }
    return Collections.<Announcement>emptyList();
}

From source file:org.eurekastreams.server.persistence.mappers.db.GetRequestsForGroupMembershipByGroup.java

/**
 * Get all flagged activities.//from   w  ww.  java 2s  .c  o  m
 * 
 * @param inRequest
 *            the request containing the start and end index
 * @return a paged set of ActivityDTOs
 */
@Override
@SuppressWarnings("unchecked")
public PagedSet<Long> execute(final GetRequestForGroupMembershipRequest inRequest) {
    final long groupId = inRequest.getGroupId();
    final int startIndex = inRequest.getStartIndex();
    final int numDesired = inRequest.getEndIndex() - startIndex + 1;

    Query q = buildQuery("pk.personId", groupId);
    q.setFirstResult(startIndex);
    q.setMaxResults(numDesired);
    List<Long> list = q.getResultList();

    // Get/compute the total count: If we didn't get all we asked for, then we know where the end of the list is,
    // and thus can calculate the total count without doing a query. Otherwise we'll have to ask.
    int totalCount;
    if (list.size() < numDesired) {
        totalCount = startIndex + list.size();
    } else {
        totalCount = ((Long) buildQuery("count(*)", groupId).getSingleResult()).intValue();
    }

    int endIndex = startIndex + list.size() - 1;

    if (log.isInfoEnabled()) {
        log.info("Request for membership requests for group " + groupId + " from " + startIndex + "-"
                + inRequest.getEndIndex() + " yielded " + list.size() + " results and a total count of "
                + totalCount + ".");
    }

    return new PagedSet<Long>(startIndex, endIndex, totalCount, list);
}