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:eu.forgestore.ws.impl.FStoreJpaController.java

@SuppressWarnings("unchecked")
public List<FStoreUser> readUsers(int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    Query q = entityManager.createQuery("SELECT m FROM FStoreUser m");
    q.setFirstResult(firstResult);//  w  w w .  j  a  va  2 s .  com
    q.setMaxResults(maxResults);
    return q.getResultList();
}

From source file:com.easyjf.core.dao.impl.GenericDAOImpl.java

public List query(final String queryStr, final Object[] params, final int begin, final int max) {
    List ret = (List) this.getJpaTemplate().execute(new JpaCallback() {

        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query query = em.createQuery(queryStr);
            int parameterIndex = 1;
            if (params != null && params.length > 0) {
                for (Object obj : params) {
                    query.setParameter(parameterIndex++, obj);
                }//  w w w . j av a 2 s .c  o  m
            }
            if (begin >= 0 && max > 0) {
                query.setFirstResult(begin);
                query.setMaxResults(max);
            }
            return query.getResultList();
        }
    });
    if (ret != null && ret.size() >= 0) {
        return ret;
    } else {
        return new ArrayList();
    }
}

From source file:com.easyjf.core.dao.impl.GenericDAOImpl.java

public List executeNativeQuery(final String nnq, final Object[] params, final int begin, final int max) {
    List ret = (List) this.getJpaTemplate().execute(new JpaCallback() {

        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query query = em.createNativeQuery(nnq);
            int parameterIndex = 1;
            if (params != null && params.length > 0) {
                for (Object obj : params) {
                    query.setParameter(parameterIndex++, obj);
                }//from ww  w.  j a va 2s. c om
            }
            if (begin >= 0 && max > 0) {
                query.setFirstResult(begin);
                query.setMaxResults(max);
            }
            return query.getResultList();
        }
    });
    if (ret != null && ret.size() >= 0) {
        return ret;
    } else {
        return new ArrayList();
    }
}

From source file:org.easy.criteria.CriteriaProcessor.java

/**
 * @param query/*from w w  w.ja  v  a2  s. c  o m*/
 * @param startIndex
 * @param maxResult
 */
private void setPagination(Query query, int startIndex, int maxResult) {
    // Pagination
    if (startIndex >= 0) {
        log.debug("startIndex = " + startIndex);
        query.setFirstResult(startIndex * maxResult);
    }

    if (maxResult > 0) {
        log.debug("maxResult = " + maxResult);
        query.setMaxResults(maxResult);
    }

}

From source file:com.easyjf.core.dao.impl.GenericDAOImpl.java

public List executeNamedQuery(final String queryName, final Object[] params, final int begin, final int max) {
    List ret = (List) this.getJpaTemplate().execute(new JpaCallback() {

        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query query = em.createNamedQuery(queryName);
            int parameterIndex = 1;
            if (params != null && params.length > 0) {
                for (Object obj : params) {
                    query.setParameter(parameterIndex++, obj);
                }/*from ww  w .  j  a  va2  s. c  o  m*/
            }
            if (begin >= 0 && max > 0) {
                query.setFirstResult(begin);
                query.setMaxResults(max);
            }
            return query.getResultList();
        }
    });
    if (ret != null && ret.size() >= 0) {
        return ret;
    } else {
        return new ArrayList();
    }
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public List<Category> readCategories(int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    Query q = entityManager.createQuery("SELECT m FROM Category m  ORDER BY m.id");
    q.setFirstResult(firstResult);//from   ww w  .  ja v a  2 s  . c  o  m
    q.setMaxResults(maxResults);
    return q.getResultList();
}

From source file:org.rhq.enterprise.server.alert.AlertDampeningManagerBean.java

@SuppressWarnings("unchecked")
private boolean shouldFireDurationCountAlert(int alertDefinitionId, int eventCountThreshold, long lastSeconds) {

    long oldestEventTime = System.currentTimeMillis() - (lastSeconds * 1000);

    Query query = entityManager.createNamedQuery(AlertDampeningEvent.QUERY_FIND_BY_TIME_AND_TYPES);
    query.setParameter("alertDefinitionId", alertDefinitionId);
    query.setParameter("eventTypes", EnumSet.of(Type.POSITIVE, Type.POSITIVE_AGAIN));
    query.setParameter("oldestEventTime", oldestEventTime);

    /*/*from w  w  w. jav  a2  s . c  o m*/
     * Make sure we get at most the number of events we need in order to trigger the DURATION_COUNT dampening rule
     */
    query.setMaxResults(eventCountThreshold);

    List<AlertDampeningEvent> oldestEvents = query.getResultList();
    deleteAlertEventsOlderThan(alertDefinitionId, oldestEventTime);

    // if we have enough, it'll be exactly equal to the number need (thanks to setMaxResults)
    boolean shouldFire = (oldestEvents.size() == eventCountThreshold);
    log.debug("Need " + eventCountThreshold + " events " + "for the last " + lastSeconds + " seconds" + ", "
            + "found " + oldestEvents.size());

    if (shouldFire) {
        for (AlertDampeningEvent event : oldestEvents) {
            event.setAlertDefinition(null);
            entityManager.remove(event);
        }
    }

    // let the caller know what happened, so they can appropriately fire an alert or not
    return shouldFire;
}

From source file:com.easyjf.core.dao.impl.GenericDAOImpl.java

public List<T> find(final String queryStr, final Object[] params, final int begin, final int max) {
    List<T> ret = (List<T>) this.getJpaTemplate().execute(new JpaCallback() {
        public Object doInJpa(EntityManager em) throws PersistenceException {

            String clazzName = clazz.getName();
            StringBuffer sb = new StringBuffer("select obj from ");
            sb.append(clazzName).append(" obj").append(" where ").append(queryStr);
            Query query = em.createQuery(sb.toString());
            int parameterIndex = 1;
            if (params != null && params.length > 0) {
                for (Object obj : params) {
                    query.setParameter(parameterIndex++, obj);
                }/*from   w  w w.  j  a v  a 2s  . c  o  m*/
            }
            if (begin >= 0 && max > 0) {
                query.setFirstResult(begin);
                query.setMaxResults(max);
            }
            if (begin >= 0 && max > 0) {
                query.setFirstResult(begin);
                query.setMaxResults(max);
            }
            return query.getResultList();
        }
    });
    if (ret != null && ret.size() >= 0) {
        return ret;
    } else {
        return new ArrayList<T>();
    }
}

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");
    }// ww  w . ja v  a2s  .  co  m
    query.setMaxResults(pageSize);
    return doPublish(query.getResultList());
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public List<FStoreProperty> readProperties(int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    Query q = entityManager.createQuery("SELECT m FROM FStoreProperty m  ORDER BY m.id");
    q.setFirstResult(firstResult);//  w  w w.  j av  a 2s.c  om
    q.setMaxResults(maxResults);
    return q.getResultList();

}