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:org.codehaus.grepo.query.jpa.generator.QueryGeneratorBase.java

protected void applyFirstResultSetting(GenericQuery genericQuery, QueryMethodParameterInfo qmpi, Query query) {
    Integer firstResult = GeneratorUtils.getFirstResult(qmpi, genericQuery);
    if (firstResult != null) {
        query.setFirstResult(firstResult);
    }//w ww.  j a  va2s .  c o m
}

From source file:org.nuxeo.ecm.platform.audit.service.LogEntryProvider.java

@SuppressWarnings("unchecked")
public List<LogEntry> nativeQueryLogs(String whereClause, int pageNb, int pageSize) {
    Query query = em.createQuery("from LogEntry log where " + whereClause);
    if (pageNb > 1) {
        query.setFirstResult((pageNb - 1) * pageSize);
    } else if (pageNb == 0) {
        log.warn("Requested pageNb equals 0 but page index start at 1. Will fallback to fetch the first page");
    }// w  w w  . jav a 2s  .  c  om
    query.setMaxResults(pageSize);
    return doPublish(query.getResultList());
}

From source file:org.springframework.integration.jpa.core.DefaultJpaOperations.java

@Override
public List<?> getResultListForNamedQuery(String selectNamedQuery, ParameterSource parameterSource,
        int firstResult, int maxNumberOfResults) {

    final Query query = entityManager.createNamedQuery(selectNamedQuery);
    setParametersIfRequired(selectNamedQuery, parameterSource, query);

    if (firstResult > 0) {
        query.setFirstResult(firstResult);
    }/*from w  ww  .  j a va  2 s.  co  m*/
    if (maxNumberOfResults > 0) {
        query.setMaxResults(maxNumberOfResults);
    }

    return query.getResultList();

}

From source file:com.mycomm.dao.mydao.base.MyDaoSupport.java

@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
public ResultHelp<T> getScrollData(int firstindex, int maxresult, String wherejpql, Object[] queryParams,
        LinkedHashMap<String, String> orderby) {
    ResultHelp qr = new ResultHelp<T>();
    String entityname = getEntityName(this.entityClass);
    Query query = entityManager.createQuery("select o from " + entityname + " o "
            + (wherejpql == null || "".equals(wherejpql.trim()) ? "" : "where " + wherejpql)
            + buildOrderby(orderby));//  w  w  w .java 2  s  . c o  m
    setQueryParams(query, queryParams);
    if (firstindex != -1 && maxresult != -1) {
        query.setFirstResult(firstindex).setMaxResults(maxresult);
    }
    qr.setResultlist(query.getResultList());
    query = entityManager.createQuery("select count(" + getCountField(this.entityClass) + ") from " + entityname
            + " o " + (wherejpql == null || "".equals(wherejpql.trim()) ? "" : "where " + wherejpql));
    setQueryParams(query, queryParams);
    qr.setTotalrecord((Long) query.getSingleResult());
    return qr;
}

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

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

From source file:com.taobao.ad.easyschedule.base.JPABaseDAO.java

public List queryForListIsNotEmpty(String queryString, final Map<String, Object> map, final PageInfo pageInfo,
        String orderBy) {// www  .j a  v a2 s  .c om
    final List<String> keyList = getKeyList(map);
    final String queryStringCallBack = getQueryString(queryString, keyList, orderBy);
    return (List) this.getJpaTemplate().execute(new JpaCallback() {
        @Override
        public List doInJpa(EntityManager em) throws PersistenceException {
            Query query = em.createQuery(queryStringCallBack);
            for (int i = 0; i < keyList.size(); i++) {
                query.setParameter((i + 1), map.get(keyList.get(i)));
            }
            // query.setFirstResult(((pageInfo.getToPage() == 0 ? 1 :
            // pageInfo.getToPage()) - 1) * pageInfo.getPerPageSize());
            query.setFirstResult(pageInfo.getStartRow());
            query.setMaxResults(pageInfo.getPerPageSize());
            return query.getResultList();
        }
    });
}

From source file:edu.umm.radonc.ca_dash.model.ActivityFacade.java

public List<ActivityAIPC> itemsDateRange(Date start, Date end, int[] range) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(ActivityAIPC.class);
    Root<ActivityAIPC> rt = cq.from(ActivityAIPC.class);
    cq.where(cb.and(rt.get(ActivityAIPC_.fromdateofservice).isNotNull(),
            cb.and(cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 528),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 529),
                    cb.notEqual(rt.get(ActivityAIPC_.procedurecodeser), 530),
                    cb.between(rt.get(ActivityAIPC_.fromdateofservice), start, end))));
    cq.orderBy(cb.asc(rt.get(ActivityAIPC_.fromdateofservice)));
    Query q = em.createQuery(cq);
    q.setMaxResults(range[1] - range[0] + 1);
    q.setFirstResult(range[0]);
    return q.getResultList();

}

From source file:org.opentides.persistence.impl.AuditLogDAOImpl.java

@SuppressWarnings("unchecked")
public final List<AuditLog> findByNamedQuery(final String name, final Map<String, Object> params, int start,
        int total) {
    String queryString = getJpqlQuery(name);
    Query queryObject = getEntityManager().createQuery(queryString);
    if (params != null) {
        for (Map.Entry<String, Object> entry : params.entrySet())
            queryObject.setParameter(entry.getKey(), entry.getValue());
    }//from  w w  w.j  av a2  s  .  co m
    if (start > -1)
        queryObject.setFirstResult(start);
    if (total > -1)
        queryObject.setMaxResults(total);
    return queryObject.getResultList();
}

From source file:cn.guoyukun.spring.jpa.repository.RepositoryHelper.java

/**
 * <p>?ql?paramsqlpageable? null?<br/>
 * ?{@see cn.guoyukun.spring.jpa.repository.UserRepository2ImplIT#testFindAll()}
 *
 * @param ql//from ww  w  . jav  a2s  . c  o  m
 * @param pageable null?
 * @param params
 * @param <M>
 * @return
 */
public <M> List<M> findAll(final String ql, final Pageable pageable, final Object... params) {

    Query query = getEntityManager()
            .createQuery(ql + prepareOrder(pageable != null ? pageable.getSort() : null));
    applyEnableQueryCache(query);
    setParameters(query, params);
    if (pageable != null) {
        query.setFirstResult(pageable.getOffset());
        query.setMaxResults(pageable.getPageSize());
    }

    return query.getResultList();
}

From source file:com.sdl.odata.datasource.jpa.JPADatasourceProvider.java

private <T> List<T> executeQueryListResult(JPAQuery jpaQuery) {
    EntityManager em = entityManagerFactory.createEntityManager();

    String queryString = jpaQuery.getQueryString();
    Query query = em.createQuery(queryString);
    int nrOfResults = jpaQuery.getLimitCount();
    int startPosition = jpaQuery.getSkipCount();
    Map<String, Object> queryParams = jpaQuery.getQueryParams();

    try {//from   ww w.  jav  a  2 s . c o m
        if (nrOfResults > 0) {
            query.setMaxResults(nrOfResults);
        }

        if (startPosition > 0) {
            query.setFirstResult(startPosition);
        }

        for (Map.Entry<String, Object> entry : queryParams.entrySet()) {
            query.setParameter(entry.getKey(), tryConvert(entry.getValue()));
        }

        return query.getResultList();
    } finally {
        em.close();
    }
}