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:fr.mby.opa.picsimpl.dao.DbProposalDao.java

@Override
public List<ProposalBag> findBagAncestry(final long branchId, final long until) {
    final EmCallback<List<ProposalBag>> emCallback = new EmCallback<List<ProposalBag>>(this.getEmf()) {

        @Override//from   w  ww  .j av a 2  s . c  o  m
        @SuppressWarnings("unchecked")
        protected List<ProposalBag> executeWithEntityManager(final EntityManager em)
                throws PersistenceException {
            final Query findLastBagQuery = em.createNamedQuery(ProposalBag.FIND_BRANCH_BAGS_UNTIL);
            findLastBagQuery.setParameter("branchId", branchId);
            findLastBagQuery.setParameter("until", until);
            findLastBagQuery.setMaxResults(DbProposalDao.PAGINATION_SIZE);

            final List<ProposalBag> branches = findLastBagQuery.getResultList();
            return branches;
        }
    };

    return emCallback.getReturnedValue();
}

From source file:utils.jpa.EntityResource.java

private List<T> getAllPriv(@BeanParam TableSearchQuery tb, List<String> fieldsSearchBy) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);

    cq.select(root);/*from w  w  w  .  java  2  s. c  om*/

    //setting order
    if (tb.getOrderType().equals(OrderType.DESC)) {
        cq.orderBy(cb.desc(root.get(tb.getOrder())));
    } else {
        cq.orderBy(cb.asc(root.get(tb.getOrder())));
    }

    Query query = tb.createQuery(em, cb, cq, root);
    if (query == null) {
        query = createCommonQuery(cb, cq, root, tb, fieldsSearchBy);
    }

    if (tb.getOffset() > 0) {
        query.setFirstResult(tb.getOffset());
    }
    if (tb.getLimit() > 0) {
        query.setMaxResults(tb.getLimit());
    }

    return 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) {//from  ww  w . j  a  va2  s  .com
    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:com.sun.socialsite.business.impl.JPANotificationManagerImpl.java

/**
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 *///from w w  w . j  a  va2 s  .  c  o  m
@SuppressWarnings(value = "unchecked")
private List<MessageContent> getNotificationsByTypeAndLabel(Profile p, String type, String label, int offset,
        int length) throws SocialSiteException {
    Query query = strategy.getNamedQuery("MessageContent.getByToProfileTypeLabel");
    query.setParameter(1, p.getUserId());
    query.setParameter(2, type);
    query.setParameter(3, label);
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return (List<MessageContent>) query.getResultList();
}

From source file:org.eurekastreams.server.persistence.FeedReaderMapper.java

/**
 * Find the domain entity by id./*from   w ww.  j a v a 2  s .  c  o  m*/
 * 
 * @param openSocialIdString
 *            List of OS Ids for the people to get Top feeds for.
 * @return the entity with the input
 */
@SuppressWarnings("unchecked")
public List<FeedReaderUrlCount> findTop10FriendFeeds(final String openSocialIdString) {
    List<FeedReaderUrlCount> feedList = new LinkedList<FeedReaderUrlCount>();

    if (!openSocialIdString.isEmpty() && openSocialIdString != null) {
        Query q = entityManager
                .createQuery("SELECT url, count(*), feedTitle from FeedReader where openSocialId IN ("
                        + openSocialIdString + ") group by url, feedTitle ORDER BY count(*) desc");
        // TODO get rid of this and figure out an appropriate place to put this.
        String theMagicNumberRuleSucks = "10";
        q.setMaxResults(Integer.parseInt(theMagicNumberRuleSucks));
        List<Object[]> results = (List<Object[]>) q.getResultList();

        for (Object[] result : results) {
            FeedReaderUrlCount resultItem = new FeedReaderUrlCount();
            resultItem.setUrl((String) result[0]);
            resultItem.setCount((Long) result[1]);
            resultItem.setFeedTitle((String) result[2]);
            feedList.add(resultItem);
        }
    }

    return feedList;
}

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

/**
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 *///from  w  ww  .  ja v a  2  s . co 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.openmeap.model.ModelServiceImpl.java

@Override
public ApplicationArchive getApplicationArchiveByDeployment(Deployment depl) {
    Query q = entityManager.createQuery("select distinct aa " + "from ApplicationArchive aa, Deployment d  "
            + "where d.applicationArchive=aa and d.id=:id ");
    q.setParameter("id", depl.getId());
    q.setMaxResults(1);
    try {/*  w  w  w  . ja  v  a2  s .c  om*/
        Object o = q.getSingleResult();
        return ((ApplicationArchive) o);
    } catch (NoResultException nre) {
        return null;
    }
}

From source file:com.openmeap.model.ModelServiceImpl.java

@Override
public Deployment getLastDeployment(Application app) {
    Query q = entityManager.createQuery("select distinct d " + "from Deployment d join d.application "
            + "where d.application.id=:id " + "order by d.createDate desc");
    q.setParameter("id", app.getId());
    q.setMaxResults(1);
    try {// w w  w  .  j  ava 2s.c  o m
        Object o = q.getSingleResult();
        return (Deployment) o;
    } catch (NoResultException nre) {
        return null;
    }
}

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  w  w  w.jav a 2s.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:es.ucm.fdi.dalgs.user.repository.UserRepository.java

@SuppressWarnings("unchecked")
public List<User> getAll(Integer pageIndex, Boolean showAll, String typeOfUser) {
    Query query = null;
    if (showAll) {
        query = em.createQuery("select u from User u join u.roles ur where ur.role =?1 order by u.id");
        query.setParameter(1, typeOfUser);

    } else {/*from   w  w  w .  j a v a 2 s . com*/
        query = em.createQuery(
                "select u from User u join u.roles ur where ur.role =?1 and u.enabled = '00000001' order by u.id");
        query.setParameter(1, typeOfUser);

    }
    return query.setMaxResults(noOfRecords).setFirstResult(pageIndex * noOfRecords).getResultList();
}