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.easyj.orm.jpa.JPAEntityDao.java

/**
 * Adiciona mapa de parmetros a query/*from   ww w . j av a 2  s.co m*/
 *
 * @param q Query que requer parmetros
 * @param params Mapa de parmetros
 * @return true se todos os parmetros foram adicionados, false se houve erro em algum parmetro
 */
private boolean setParameters(Query q, Map<String, Object> params) {
    if (q != null && params != null) {
        Integer maxResults = (Integer) params.remove(EntityService.PARAM_MAX_RESULTS);
        if (maxResults != null && maxResults > 0) {
            q.setMaxResults(maxResults.intValue());
        }

        Integer startPosition = (Integer) params.remove(EntityService.PARAM_START_POSITION);
        if (startPosition != null && startPosition > -1) {
            q.setFirstResult(startPosition.intValue());
        }

        for (Entry<String, Object> o : params.entrySet()) {
            try {
                q.setParameter(o.getKey().trim(), o.getValue());
            } catch (IllegalArgumentException ex) {
                logger.debug("Illegal Query Parameter", ex);
                return false;
            }
        }
    }
    return true;
}

From source file:org.apache.roller.planet.business.jpa.JPAPlanetManagerImpl.java

public List getEntries(PlanetGroup group, Date startDate, Date endDate, int offset, int len)
        throws PlanetException {
    StringBuffer queryString = new StringBuffer();

    if (group == null) {
        throw new PlanetException("group cannot be null or empty");
    }//w  ww .j  av  a  2 s .  c o  m

    List ret = null;
    try {
        long startTime = System.currentTimeMillis();

        StringBuffer sb = new StringBuffer();
        List params = new ArrayList();
        int size = 0;
        sb.append("SELECT e FROM SubscriptionEntry e ");
        sb.append("JOIN e.subscription.groups g ");

        params.add(size++, group.getHandle());
        sb.append("WHERE g.handle = ?").append(size);

        if (startDate != null) {
            params.add(size++, new Timestamp(startDate.getTime()));
            sb.append(" AND e.pubTime > ?").append(size);
        }
        if (endDate != null) {
            params.add(size++, new Timestamp(endDate.getTime()));
            sb.append(" AND e.pubTime < :?").append(size);
        }
        sb.append(" ORDER BY e.pubTime DESC");

        Query query = strategy.getDynamicQuery(sb.toString());
        for (int i = 0; i < params.size(); i++) {
            query.setParameter(i + 1, params.get(i));
        }
        if (offset > 0) {
            query.setFirstResult(offset);
        }
        if (len != -1) {
            query.setMaxResults(len);
        }

        ret = query.getResultList();

        long endTime = System.currentTimeMillis();

        log.debug("Generated aggregation of " + ret.size() + " in " + ((endTime - startTime) / 1000.0)
                + " seconds");

    } catch (Throwable e) {
        throw new PlanetException(e);
    }

    return ret;
}

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

public List<TxInstance> itemsDateRange(Date startDate, Date endDate, int[] range) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(TxInstance.class);
    Root<TxInstance> rt = cq.from(TxInstance.class);
    cq.where(cb.and(rt.get(TxInstance_.completed).isNotNull(),
            cb.between(rt.get(TxInstance_.completed), startDate, endDate)));
    cq.orderBy(cb.asc(rt.get(TxInstance_.completed)));
    Query q = em.createQuery(cq);
    q.setMaxResults(range[1] - range[0] + 1);
    q.setFirstResult(range[0]);/*  w w w .  jav a 2 s .  c  o m*/
    return q.getResultList();
}

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

public List<?> nativeQuery(String queryString, Map<String, Object> params, int pageNb, int pageSize) {
    if (pageSize <= 0) {
        pageSize = 1000;/*  w  ww .  ja va  2  s.c  o m*/
    }
    Query query = em.createQuery(queryString);
    for (Entry<String, Object> en : params.entrySet()) {
        query.setParameter(en.getKey(), en.getValue());
    }
    if (pageNb > 1) {
        query.setFirstResult((pageNb - 1) * pageSize);
    }
    query.setMaxResults(pageSize);
    return doPublishIfEntries(query.getResultList());
}

From source file:net.kamhon.ieagle.dao.Jpa2Dao.java

@SuppressWarnings("unchecked")
public List<T> findBlock(String query, final int offset, final int recordCount, final Object... objectArray) {
    final String query2 = convertJpaPositionParams(query);
    Query q = em.createQuery(query2);
    for (int i = 0; i < objectArray.length; i++) {
        q.setParameter(i + 1, objectArray[i]);
    }//from  w  ww. j a va2s .  co m
    q.setFirstResult(offset);
    q.setMaxResults(recordCount);
    return q.getResultList();
}

From source file:org.apache.falcon.state.store.jdbc.JDBCStateStore.java

@Override
public InstanceState getLastExecutionInstance(Entity entity, String cluster) throws StateStoreException {
    String key = new EntityClusterID(entity, cluster).getEntityID().getKey();
    EntityManager entityManager = getEntityManager();
    Query q = entityManager.createNamedQuery(PersistenceConstants.GET_LAST_INSTANCE_FOR_ENTITY_CLUSTER);
    q.setParameter("entityId", key);
    q.setParameter("cluster", cluster);
    q.setMaxResults(1);
    List result = q.getResultList();
    entityManager.close();//  w  w w  .ja  v a2  s .c  o  m
    if (!result.isEmpty()) {
        try {
            return BeanMapperUtil.convertToInstanceState((InstanceBean) result.get(0));
        } catch (IOException e) {
            throw new StateStoreException(e);
        }
    }
    return null;
}

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

@SuppressWarnings("unchecked")
@Override/* www.j av  a 2s. c o m*/
public List<FederationRecordIndexingRequired> listMarkedForUpdateIndex(RecordCollection collection,
        int maxResults) {
    List<FederationRecordIndexingRequired> markedForUpdateIndex = new ArrayList<FederationRecordIndexingRequired>();
    if (collection.isFederationOwner()) {
        int remainingMaxResults = maxResults - markedForUpdateIndex.size();
        String ql = "FROM FederationRecordIndexingRequired frir WHERE frir.ownerCollection = :ownerCollection";
        Query query = this.getEntityManager().createQuery(ql);
        query.setParameter("ownerCollection", collection);
        if (maxResults >= 0) {
            query.setMaxResults(remainingMaxResults);
        }
        List<FederationRecordIndexingRequired> federationIndexingRequired = query.getResultList();
        for (FederationRecordIndexingRequired indexingRequired : federationIndexingRequired) {
            markedForUpdateIndex.add(indexingRequired);
        }
    }
    return markedForUpdateIndex;
}

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

@SuppressWarnings("unchecked")
@Override//from  ww w  .  j a v  a  2 s. com
public List<FederationRecordDeletionRequired> listMarkedForExclusionOrDeletion(RecordCollection collection,
        int maxResults) {
    List<FederationRecordDeletionRequired> markedForExclusionOrDeletion = new ArrayList<FederationRecordDeletionRequired>();
    if (collection.isFederationOwner()) {
        int remainingMaxResults = maxResults - markedForExclusionOrDeletion.size();
        String ql = "FROM FederationRecordDeletionRequired frdr WHERE frdr.ownerCollection = :ownerCollection";
        Query query = this.getEntityManager().createQuery(ql);
        query.setParameter("ownerCollection", collection);
        if (maxResults >= 0) {
            query.setMaxResults(remainingMaxResults);
        }
        List<FederationRecordDeletionRequired> federationDeletionRequired = query.getResultList();
        for (FederationRecordDeletionRequired deletionRequired : federationDeletionRequired) {
            markedForExclusionOrDeletion.add(deletionRequired);
        }
    }
    return markedForExclusionOrDeletion;
}

From source file:net.kamhon.ieagle.dao.JpaDao.java

@SuppressWarnings("unchecked")
@Override/*  w  w w .jav  a  2s  .c  om*/
public List<T> findBlock(String query, final int offset, final int recordCount, final Object... objectArray) {
    final String query2 = convertJpaPositionParams(query);
    return getJpaTemplate().executeFind(new JpaCallback<List<T>>() {
        @Override
        public List<T> doInJpa(EntityManager em) throws PersistenceException {
            Query q = em.createQuery(query2);
            for (int i = 0; i < objectArray.length; i++) {
                q.setParameter(i + 1, objectArray[i]);
            }
            q.setFirstResult(offset);
            q.setMaxResults(recordCount);
            return q.getResultList();
        }
    });
}

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

@SuppressWarnings("unchecked")
@Override/* www  . j a v  a2  s.c  o m*/
public List<Record> listLastTraversedRecords(ConnectorInstance connectorInstance, int maxSize) {
    String ql = "FROM Record r WHERE r.connectorInstance = :connectorInstance ORDER BY lastFetched DESC";
    Query query = this.getEntityManager().createQuery(ql);
    query.setParameter("connectorInstance", connectorInstance);
    query.setMaxResults(maxSize);
    List<Record> results = (List<Record>) query.getResultList();
    return results;
}