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:com.eu.evaluation.server.dao.AbstractDAO.java

/**
 * //from  w ww. j av a 2  s .c  o m
 *
 * @param jpql
 * @param params
 * @param pageNo
 * @param pageSize
 * @return
 */
public PageData<T> query(String jpql, MapSqlParameterSource params, int pageNo, int pageSize) {
    // Count
    String countQueryString = " select count (*) " + removeSelect(removeOrders(jpql));
    Query countQuery = createQuery(countQueryString, params);
    Long totalCount = (Long) countQuery.getSingleResult();
    if (totalCount < 1) {
        return new PageData<T>();
    }

    // 
    int startIndex = PageData.getStartOfPage(pageNo, pageSize);
    Query query = createQuery(jpql, params);
    query.setFirstResult(startIndex);
    query.setMaxResults(pageSize);
    List<T> rows = query.getResultList();

    return new PageData<T>(startIndex, totalCount, pageSize, rows);
}

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

@Override
@Transactional/*  w  w w . j  a va  2  s  . c o m*/
public List<MDOSong> getAllOrderByMostPlayed(String username, int max) {
    Query query = entityManager.createQuery(
            "from MDOSong as a where (a.owner.login = :userName) and (a.statistics.timesplayed>0) ORDER BY (a.statistics.timesplayed) DESC");
    query.setParameter("userName", username);
    query.setMaxResults(max);

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

From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java

/**
 * find entity in the range//  w w  w.  j  ava  2s  .  c  o m
 *
 * @param range
 * @return
 */
public List<T> findRange(int[] range) {
    EntityManager em = getEntityManager();
    CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
    cq.select(cq.from(entityClass));
    Query q = em.createQuery(cq);
    q.setMaxResults(range[1] - range[0]);
    q.setFirstResult(range[0]);
    return q.getResultList();
}

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

@SuppressWarnings("unchecked")
public final List<AuditLog> findAll(int start, int total) {
    Query query = getEntityManager().createQuery("from AuditLog obj");
    if (start > -1)
        query.setFirstResult(start);/*from w  w w. j  a  v a 2s  .  co  m*/
    if (total > -1)
        query.setMaxResults(total);
    return query.getResultList();
}

From source file:fr.mby.opa.picsimpl.dao.DbPictureDao.java

@Override
@SuppressWarnings("unchecked")
public List<Picture> findAllPictures(final Long since) {
    final Timestamp sinceTimstamp = new Timestamp((since != null) ? since : 0L);

    final EmCallback<List<Picture>> emCallback = new EmCallback<List<Picture>>(this.getEmf()) {

        @Override/*from  w w w.j a  va 2s  . c  o m*/
        protected List<Picture> executeWithEntityManager(final EntityManager em) throws PersistenceException {
            final Query findAllQuery = em.createNamedQuery(Picture.FIND_ALL_PICTURES_ORDER_BY_DATE);
            findAllQuery.setParameter("since", sinceTimstamp);
            findAllQuery.setMaxResults(DbPictureDao.PAGINATION_SIZE);

            return findAllQuery.getResultList();
        }
    };

    List<Picture> pictures = emCallback.getReturnedValue();

    if (pictures == null) {
        pictures = Collections.emptyList();
    }

    return pictures;
}

From source file:org.easyj.orm.jpa.SingleJPAEntityDao.java

/**
 * Binds parameter map to the query./*from  ww w .j a v a2  s .  c  o m*/
 * 
 * There are two special parameters that is of use:
 * {@link SingleService.PARAM_MAX_RESULTS} used to limit maximum results returned
 * {@link SingleService.PARAM_START_POSITION} used to tell the starting position the result should start
 *
 * @param q query to bind parameters
 * @param params parameter map to bind into the query
 * @return true if all parameters where bound successfully, otherwise false
 */
private boolean setParameters(Query q, Map<String, Object> params) {
    if (q != null && params != null) {
        Integer maxResults = (Integer) params.remove(SingleDao.PARAM_MAX_RESULTS);
        if (maxResults != null && maxResults > 0) {
            q.setMaxResults(maxResults.intValue());
        }

        Integer startPosition = (Integer) params.remove(SingleDao.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.asqatasun.entity.dao.subject.WebResourceDAOImpl.java

@Override
public List<WebResource> findWebResourceFromItsParent(WebResource webResource, int start, int chunkSize) {
    Query query = entityManager.createQuery("SELECT wr FROM " + getEntityClass().getName() + " wr"
            + " JOIN wr.parent p" + " WHERE p=:webResource");
    query.setParameter("webResource", webResource);
    query.setFirstResult(start);//w w  w .j  a  v  a 2 s.  co  m
    query.setMaxResults(chunkSize);
    return (List<WebResource>) query.getResultList();
}

From source file:org.medici.bia.dao.titleoccslist.TitleOccsListDAOJpaImpl.java

/**
 * {@inheritDoc}//from w  w  w .ja  va2s .c  o  m
 */
@Override
public TitleOccsList findTitleOcc(String titleOcc) throws PersistenceException {
    Query query = getEntityManager().createQuery("FROM TitleOccsList WHERE titleOcc LIKE :titleOcc");
    query.setParameter("titleOcc", titleOcc);
    query.setMaxResults(1);
    if (query.getResultList().size() != 0) {
        return (TitleOccsList) query.getSingleResult();
    }
    return null;
}

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

/**
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 *//*from w w  w. j  a  v  a  2  s . c o m*/
@SuppressWarnings(value = "unchecked")
public List<SocialSiteActivity> getActivitiesByGroup(Group group, int offset, int length)
        throws SocialSiteException {
    if (group == null) {
        throw new SocialSiteException("Group cannot be null");
    }
    Query query = strategy.getNamedQuery("Activity.getByGroup");
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    query.setParameter(1, group);
    return (List<SocialSiteActivity>) query.getResultList();
}

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

/**
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 *///from  ww w  .j  a  va  2s.  co m
@SuppressWarnings(value = "unchecked")
public List<SocialSiteActivity> getActivitiesByGadget(String gadgetId, int offset, int length)
        throws SocialSiteException {
    if (gadgetId == null) {
        throw new SocialSiteException("Gadget ID is null");
    }
    Query query = strategy.getNamedQuery("Activity.getByGadget");
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    query.setParameter(1, gadgetId);
    return (List<SocialSiteActivity>) query.getResultList();
}