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.thingsplode.TestBaseWithRepos.java

/**
 * Executes a native SQL query for checking test results.
 *
 * @param sqlQuery the native sql query you need to execute (use "?" for
 * parameters)//w ww .jav  a  2s .c o  m
 * @param maxResults the max. number of results to be returned (in case the
 * database contains 100 rows, but you limit here to 10, it will return the
 * first 10 rows)
 * @param paramValues the parameter values for the query. you can use null,
 * if the query has no parameters, or you need to specify in the order the
 * query references them.
 * @return
 */
protected List<Object> getNativeSQLQueryResult(final String sqlQuery, final int maxResults,
        final String... paramValues) {
    Query q = entityManager.createNativeQuery(sqlQuery);
    if (paramValues != null && paramValues.length > 0) {
        int i = 0;
        for (String param : paramValues) {
            q.setParameter(i, param);
            i += 1;
        }
    }
    q.setMaxResults(maxResults);
    return q.getResultList();
}

From source file:org.messic.server.datamodel.jpaimpl.DAOJPASong.java

@Override
@Transactional//from  www .  j  a  va 2s . c o m
public List<MDOSong> getRandom(String username, int max) {
    Query query = entityManager
            .createQuery("from MDOSong as a where (1=1 AND a.owner.login = :userName) order by rand()");
    query.setParameter("userName", username);
    query.setMaxResults(max);

    @SuppressWarnings("unchecked")
    List<MDOSong> results = query.getResultList();
    return results;
}

From source file:org.syncope.core.persistence.dao.impl.TaskDAOImpl.java

@Override
public <T extends Task> List<T> findAll(final int page, final int itemsPerPage, final Class<T> reference) {

    final Query query = entityManager.createQuery(buildfindAllQuery(reference).toString());

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

    if (itemsPerPage > 0) {
        query.setMaxResults(itemsPerPage);
    }//from w  ww  . j a v a2 s .c  o m

    return query.getResultList();
}

From source file:gwap.rest.UserPicture.java

protected List<ArtResource> getRandomPictures(String count, String deviceId, String origin) {
    Query query = entityManager.createNamedQuery("artResource.getRandomPictures");
    query.setParameter("deviceId", deviceId);
    query.setParameter("origin", origin);
    query.setMaxResults(Integer.parseInt(count));
    List<ArtResource> artResources = query.getResultList();
    return artResources;
}

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

public Object executeSingleIsNotEmpty(String queryString, final Map<String, Object> map, String orderBy) {

    final List<String> keyList = getKeyList(map);
    final String queryStringCallBack = getQueryString(queryString, keyList, orderBy);
    return this.getJpaTemplate().execute(new JpaCallback() {

        @Override//from w  ww  .  j a v a2s.  c o  m
        public Object 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.setMaxResults(1);
            try {
                return query.getSingleResult();
            } catch (Exception e) {
                return null;
            }
        }
    });

}

From source file:com.healthcit.cacure.dao.FormDao.java

/**
 * <b>formId</b> is id of target item.
 * @param formId Long/*from   w  w w  .  jav a2  s .c  o m*/
 * @param ordType ItemOrderingAction
 * @return pair of two consecutive QuestionnaireForm items
 */
@SuppressWarnings("unchecked")
public List<QuestionnaireForm> getAdjacentPairOfForms(Long formId, ItemOrderingAction ordType) {
    String sign = (ordType == ItemOrderingAction.UP ? "<=" : ">=");
    String orderBy = (ordType == ItemOrderingAction.UP ? "DESC" : "ASC");
    String jpql = "select otherFrm from QuestionnaireForm ordFrm, QuestionnaireForm otherFrm "
            + "where ordFrm.id = :formId " + "and otherFrm.module.id = ordFrm.module.id " + "and otherFrm.ord "
            + sign + " ordFrm.ord " + "order by otherFrm.ord " + orderBy;

    Query query = em.createQuery(jpql);
    query.setParameter("formId", formId);
    query.setMaxResults(2);
    return query.getResultList();
}

From source file:net.urlgrey.mythpodcaster.dao.MythRecordingsDAOImpl.java

@SuppressWarnings("unchecked")
@Override//from w w w  .ja v  a 2  s  .  c  o  m
@Transactional(readOnly = true)
public RecordedSeries findRecordedSeries(String seriesId, int numberOfMostRecentRecordings) {
    final Query namedQuery = entityManager.createNamedQuery("MYTH_RECORDINGS.findRecordedSeries");
    namedQuery.setParameter("seriesId", seriesId);

    final List<RecordedSeries> seriesResultsList = namedQuery.getResultList();
    if (seriesResultsList != null && seriesResultsList.size() == 1) {
        final Query seriesProgramsQuery = entityManager
                .createNamedQuery("MYTH_RECORDINGS.findRecordedProgramsForSeries");
        seriesProgramsQuery.setParameter("seriesId", seriesId);
        seriesProgramsQuery.setMaxResults(numberOfMostRecentRecordings);

        final RecordedSeries series = seriesResultsList.get(0);
        series.setRecordedPrograms(seriesProgramsQuery.getResultList());
        return series;
    }

    return null;
}

From source file:org.cleverbus.core.common.asynch.queue.MessagePollExecutorTest.java

@SuppressWarnings("unchecked")
@Nullable//from  w ww  .j a v  a  2 s  .co m
private Message findMessage(String correlationId) {
    String jSql = "SELECT m " + "FROM Message m " + "WHERE m.correlationId = ?1";

    Query q = em.createQuery(jSql);
    q.setParameter(1, correlationId);
    q.setMaxResults(1);
    List<Message> messages = (List<Message>) q.getResultList();

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

From source file:cn.guoyukun.spring.jpa.repository.callback.DefaultSearchCallback.java

public void setPageable(Query query, Searchable search) {
    if (search.hasPageable()) {
        Pageable pageable = search.getPage();
        query.setFirstResult(pageable.getOffset());
        query.setMaxResults(pageable.getPageSize());
    }//from  www . j ava 2  s. c  o  m
}

From source file:org.orcid.persistence.dao.impl.OrgDisambiguatedDaoImpl.java

@SuppressWarnings("unchecked")
@Override//www.  j  ava  2 s  . co  m
@Cacheable("orgs")
public List<OrgDisambiguatedEntity> getOrgs(String searchTerm, int firstResult, int maxResults) {
    String qStr = "select od.*, COUNT(*) as countAll from org_disambiguated od left join org_affiliation_relation oa on od.id = oa.org_id"
            + " where lower(name) like '%' || lower(:searchTerm) || '%' group by od.id "
            + " order by position(lower(:searchTerm) in lower(name)), char_length(name), countAll DESC, od.name";

    Query query = entityManager.createNativeQuery(qStr, OrgDisambiguatedEntity.class);
    query.setParameter("searchTerm", searchTerm);
    query.setFirstResult(firstResult);
    query.setMaxResults(maxResults);

    return query.getResultList();
}