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:org.opentides.dao.impl.BaseEntityDaoJpaImpl.java

/**
 * {@inheritDoc}//  w  w  w .  java2s  . co m
 */
@Override
@SuppressWarnings("unchecked")
public final List<T> findByExample(final T example, boolean exactMatch, int start, int total) {
    String joinClause = appendJoinToExample(example);
    String whereClause = CrudUtil.buildJpaQueryString(example, exactMatch);
    String orderClause = " " + appendOrderToExample(example);
    String filterClause = this.buildSecurityFilterClause(example);
    String append = appendClauseToExample(example, exactMatch);
    whereClause = doSQLAppend(whereClause, append);
    whereClause = doSQLAppend(whereClause, filterClause);
    if (_log.isDebugEnabled())
        _log.debug("QBE >> " + whereClause + orderClause);
    String sql = "select (obj) from " + getEntityBeanType().getName() + " obj " + joinClause + whereClause
            + orderClause;
    Query query = getEntityManager().createQuery(sql);
    setQueryParameters(query, sql, example);
    setHints(example, query);
    if (start > -1)
        query.setFirstResult(start);
    if (total > -1)
        query.setMaxResults(total);
    return query.getResultList();
}

From source file:com.sun.socialsite.business.impl.JPAGroupManagerImpl.java

/**
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 *//*w  ww .  j a v a  2 s.  c o  m*/
@SuppressWarnings(value = "unchecked")
public List<GroupRelationship> getMembershipsByProfile(Profile profile, int offset, int length)
        throws SocialSiteException {
    Query query = strategy.getNamedQuery("GroupRelationship.getByUserProfile");
    query.setParameter(1, profile);
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return (List<GroupRelationship>) query.getResultList();
}

From source file:nl.strohalm.cyclos.utils.database.DatabaseQueryHandler.java

/**
 * Applies the result limits to the given query
 *//*from w w  w. jav a  2 s . co  m*/
public void applyPageParameters(final PageParameters pageParameters, final Query query) {
    Integer firstResult = pageParameters == null ? null : pageParameters.getFirstResult();
    if (firstResult != null && firstResult >= 0) {
        query.setFirstResult(firstResult);
    }
    Integer maxResults = pageParameters == null ? null : pageParameters.getMaxResults();
    if (maxResults != null && maxResults > 0) {
        query.setMaxResults(maxResults);
    }
}

From source file:com.impetus.ankush.common.dao.impl.GenericDaoJpa.java

@SuppressWarnings(UNCHECKED)
public List<T> getAllByDisjunctionveNormalQuery(List<Map<String, Object>> disjunctionMaps, int start,
        int maxResults, String... orderBy) {

    List<LinkedHashMap<String, Object>> orderedMaps = this.getOrderedMap(disjunctionMaps);

    StringBuilder builder = createDisjunctionNormalQueryString(orderedMaps)
            .append(this.createOrderByClause(orderBy));

    Query query = this.entityManager.createQuery(builder.toString());

    // fill parameters
    this.assignQueryParameters(query, this.flattenParameterList(orderedMaps));
    query.setFirstResult(start);// w w  w .  ja  v a2 s.  c om
    query.setMaxResults(maxResults);

    return query.getResultList();
}

From source file:org.apache.syncope.core.persistence.jpa.dao.JPATaskDAO.java

@Override
@SuppressWarnings("unchecked")
public <T extends Task> List<T> findAll(final TaskType type, final ExternalResource resource,
        final Notification notification, final AnyTypeKind anyTypeKind, final String entityKey, final int page,
        final int itemsPerPage, final List<OrderByClause> orderByClauses) {

    StringBuilder queryString = buildFindAllQuery(type, resource, notification, anyTypeKind, entityKey)
            .append(toOrderByStatement(getEntityReference(type), orderByClauses));

    Query query = entityManager().createQuery(queryString.toString());
    if (resource != null) {
        query.setParameter("resource", resource);
    }// w w  w  .  j a  v  a  2s  . co m
    if (notification != null) {
        query.setParameter("notification", notification);
    }
    if (anyTypeKind != null && entityKey != null) {
        query.setParameter("anyTypeKind", anyTypeKind);
        query.setParameter("entityKey", entityKey);
    }

    query.setFirstResult(itemsPerPage * (page <= 0 ? 0 : page - 1));

    if (itemsPerPage > 0) {
        query.setMaxResults(itemsPerPage);
    }

    return query.getResultList();
}

From source file:org.asqatasun.entity.dao.audit.ContentDAOImpl.java

@Override
public List<Content> findOrphanContentList(WebResource webResource, int start, int chunkSize) {
    if (webResource instanceof Page) {
        Query query = entityManager.createQuery("SELECT distinct s FROM " + SSPImpl.class.getName() + " s"
                + JOIN_WR + WEB_RESOURCE_CONDITION + HTTP_STATUS_CONDITION);
        query.setParameter(WEB_RESOURCE_KEY, webResource);
        query.setParameter(HTTP_STATUS_CODE_KEY, DEFAULT_HTTP_STATUS_VALUE);
        query.setFirstResult(start);/*from  w  w w  .j  av a  2  s. co  m*/
        query.setMaxResults(chunkSize);
        return (List<Content>) query.getResultList();
    } else if (webResource instanceof Site) {
        Query query = entityManager.createQuery("SELECT distinct s FROM " + SSPImpl.class.getName() + " s"
                + JOIN_WR + " JOIN w.parent p" + " WHERE p=:webResource" + HTTP_STATUS_CONDITION);
        query.setParameter(WEB_RESOURCE_KEY, webResource);
        query.setParameter(HTTP_STATUS_CODE_KEY, DEFAULT_HTTP_STATUS_VALUE);
        query.setFirstResult(start);
        query.setMaxResults(chunkSize);
        List<Content> contentList = (List<Content>) query.getResultList();
        flushAndCloseEntityManager();
        return contentList;
    }
    return Collections.emptyList();
}

From source file:org.apache.roller.weblogger.business.jpa.JPARefererManagerImpl.java

/**
 * Returns hot weblogs as StatCount objects, in descending order by today's
 * hits./*w w w.ja  v a2  s . c om*/
 * @param sinceDays Restrict to last X days (or -1 for all)
 * @param offset Offset into results (for paging)
 * @param length Maximum number of results to return (for paging)
 * @return List of StatCount objects.
 */
public List getHotWeblogs(int sinceDays, int offset, int length) throws WebloggerException {

    String msg = "Getting hot weblogs";
    List results = new ArrayList();
    Calendar cal = Calendar.getInstance();
    cal.setTime(new Date());
    cal.add(Calendar.DATE, -1 * sinceDays);
    Date startDate = cal.getTime();

    if (length == -1) {
        length = Integer.MAX_VALUE - offset;
    }

    Query q = strategy.getNamedQuery(
            "WeblogReferrer.getHotWeblogsByWebsite.enabled&Website.active&Website.lastModifiedGreater");

    if (offset != 0 || length != -1) {
        q.setFirstResult(offset);
        q.setMaxResults(length);
    }
    Timestamp start = new Timestamp(startDate.getTime());
    q.setParameter(1, Boolean.TRUE);
    q.setParameter(2, Boolean.TRUE);
    q.setParameter(3, start);
    List queryResults = (List) q.getResultList();
    for (Iterator it = queryResults.iterator(); it.hasNext();) {
        Object[] row = (Object[]) it.next();
        long hits = ((Number) row[0]).longValue();
        String websiteId = (String) row[1];
        String websiteName = (String) row[2];
        String websiteHandle = (String) row[3];
        results.add(new StatCount(websiteId, websiteHandle, websiteName, "statCount.weblogDayHits", hits));
    }
    // Original query ordered by desc hits.
    // JPA QL doesn't allow queries to be ordered by agregates; do it in memory
    Collections.sort(results, statCountCountReverseComparator);

    return results;
}

From source file:org.opens.tanaguru.entity.dao.audit.ContentDAOImpl.java

@Override
public List<Content> findOrphanContentList(WebResource webResource, int start, int chunkSize) {
    if (webResource instanceof Page) {
        Query query = entityManager.createQuery("SELECT distinct s FROM " + SSPImpl.class.getName() + " s"
                + JOIN_WR + WEB_RESOURCE_CONDITION + HTTP_STATUS_CONDITION);
        query.setParameter(WEB_RESOURCE_KEY, webResource);
        query.setParameter(HTTP_STATUS_CODE_KEY, DEFAULT_HTTP_STATUS_VALUE);
        query.setFirstResult(start);/*w w  w  .  j  a v a2  s.c om*/
        query.setMaxResults(chunkSize);
        return (List<Content>) query.getResultList();
    } else if (webResource instanceof Site) {
        Query query = entityManager.createQuery("SELECT distinct s FROM " + SSPImpl.class.getName() + " s"
                + JOIN_WR + " JOIN w.parent p" + " WHERE p=:webResource" + HTTP_STATUS_CONDITION);
        query.setParameter(WEB_RESOURCE_KEY, webResource);
        query.setParameter(HTTP_STATUS_CODE_KEY, DEFAULT_HTTP_STATUS_VALUE);
        query.setFirstResult(start);
        query.setMaxResults(chunkSize);
        List<Content> contentList = (List<Content>) query.getResultList();
        flushAndCloseEntityManager();
        return contentList;
    }
    return new ArrayList<Content>();
}

From source file:com.impetus.ankush.common.dao.impl.GenericDaoJpa.java

@SuppressWarnings(UNCHECKED)
public List<T> getAllOfOrMatch(Map<String, Object> queryMap, int start, int maxResults, String... orderBy) {
    LinkedHashMap<String, Object> orderedMap = this.getOrderedMap(queryMap);

    StringBuilder builder = createSelectOrQueryString(orderedMap).append(this.createOrderByClause(orderBy));

    Query query = this.entityManager.createQuery(builder.toString());

    // fill parameters
    this.assignQueryParameters(query, this.flattenParameterList(Collections.singletonList(orderedMap)));
    query.setFirstResult(start);/*from   www .j  a v  a 2  s  .co  m*/
    query.setMaxResults(maxResults);

    return query.getResultList();
}

From source file:com.impetus.ankush.common.dao.impl.GenericDaoJpa.java

@SuppressWarnings(UNCHECKED)
public List<T> getAllByNamedQuery(String queryName, Map<String, Object> propertyValueMap, int start,
        int maxResults) {
    Query query = getEntityManager().createNamedQuery(queryName);
    for (Entry<String, Object> entry : propertyValueMap.entrySet()) {
        query.setParameter(entry.getKey(), entry.getValue());
    }//from w ww.  ja va  2s.com
    query.setFirstResult(start);
    query.setMaxResults(maxResults);
    return query.getResultList();
}