Example usage for javax.persistence Query setFirstResult

List of usage examples for javax.persistence Query setFirstResult

Introduction

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

Prototype

Query setFirstResult(int startPosition);

Source Link

Document

Set the position of the first result to retrieve.

Usage

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);
    query.setMaxResults(maxResults);//from   w  ww  . j  a v a2 s.c o m

    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);
        query.setMaxResults(chunkSize);//  w  w w. j  ava 2s. c o m
        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.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);
        query.setMaxResults(chunkSize);/*from  w w w .ja  v a  2 s.  c  o m*/
        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.EMPTY_LIST;
}

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

/**
 * Returns hot weblogs as StatCount objects, in descending order by today's
 * hits./*ww  w.  j  a v  a  2  s.co  m*/
 * @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:net.firejack.platform.core.store.BaseStore.java

@Transactional(readOnly = true)
protected List<E> findByQuery(final Integer offset, final Integer limit, final String queryStr,
        final Map queryParams, final ResultTransformer transformer) {
    return getHibernateTemplate().execute(new HibernateCallback<List<E>>() {
        public List<E> doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery(queryStr);
            if (offset != null && offset > -1) {
                query.setFirstResult(offset);
            }//from   w  ww  .j a  v a  2  s  . c  om
            if (limit != null && limit > -1) {
                query.setMaxResults(limit);
            }
            query.setProperties(queryParams);
            query.setResultTransformer(transformer);
            return query.list();
        }
    });
}

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);
        query.setMaxResults(chunkSize);//from w w  w  .ja  va2s  .co m
        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);
    query.setMaxResults(maxResults);//from  ww  w.  ja  v a  2  s  .c  o m

    return query.getResultList();
}

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

/**
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 *///from w w  w  .  ja  va2s .  co m
@SuppressWarnings(value = "unchecked")
public List<GroupRelationship> getAdminsOfGroup(Group group, int offset, int length)
        throws SocialSiteException {
    Query query = strategy.getNamedQuery("GroupRelationship.getAdminsOfGroup");
    query.setParameter(1, group);
    query.setParameter(2, GroupRelationship.Relationship.ADMIN);
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return (List<GroupRelationship>) query.getResultList();
}

From source file:org.sofun.core.team.TeamServiceImpl.java

@Override
public List<Kup> getKupsFor(Team team, int offset, int size) throws CoreException {

    final Query query = em.createNativeQuery(
            "SELECT * FROM kups WHERE id IN (SELECT DISTINCT kup_id FROM teams_kups WHERE team_id=:team_id) and status != -1 ORDER BY status ASC, start_date ASC",
            KupImpl.class);
    query.setParameter("team_id", team.getId());
    query.setFirstResult(offset);
    query.setMaxResults(size);//  w ww.  j  a v  a  2s  .c  o  m

    @SuppressWarnings("unchecked")
    List<Kup> results = query.getResultList();
    if (results != null) {
        return results;
    }
    return new ArrayList<Kup>();

}

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  . j av a 2s . c om*/
    query.setFirstResult(start);
    query.setMaxResults(maxResults);
    return query.getResultList();
}