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:de.iai.ilcd.model.dao.UserDao.java

@SuppressWarnings("unchecked")
public List<User> getUsers(Organization org, Integer first, Integer pageSize) {
    if (org == null) {
        return null;
    }//  w  w w .  jav  a2  s . co m
    EntityManager em = PersistenceUtil.getEntityManager();

    try {
        Query q = em.createQuery(
                "SELECT DISTINCT u FROM User u WHERE u.organization.id = :orgId ORDER BY u.userName");
        q.setParameter("orgId", org.getId());
        if (first != null) {
            q.setFirstResult(first.intValue());
        }
        if (pageSize != null) {
            q.setMaxResults(pageSize.intValue());
        }
        return (List<User>) q.getResultList();
    } catch (Exception e) {
        return null;
    }
}

From source file:org.broadleafcommerce.inventory.dao.InventoryDaoImpl.java

@SuppressWarnings("unchecked")
@Override// ww  w .ja  va 2s. c  o  m
public List<Inventory> readSkuByPriceAndCurrency(BigDecimal amount, BroadleafCurrency currency,
        List<FulfillmentLocation> locations, int start, int size) {
    Query query = em.createNamedQuery("SSB_READ_INVS_BY_PRICE_CURRENCY");
    query.setParameter("price", amount);
    query.setParameter("currency", currency);
    query.setParameter("locations", locations);
    query.setFirstResult(start);
    query.setMaxResults(size);
    return query.getResultList();
}

From source file:nl.b3p.kaartenbalie.core.server.accounting.AccountManager.java

/**
 * /*from   w ww  . ja v  a 2s .co m*/
 * @param firstResult
 * @param listMax
 * @param transactionType
 * @return
 */
public List getTransactions(int firstResult, int listMax, int type) throws Exception {
    Object identity = null;
    try {
        identity = MyEMFDatabase.createEntityManager(MyEMFDatabase.MAIN_EM);
        log.debug("Getting entity manager ......");
        EntityManager em = MyEMFDatabase.getEntityManager(MyEMFDatabase.MAIN_EM);

        List resultList = null;
        StringBuffer q = new StringBuffer();
        q.append("FROM Transaction AS t ");
        q.append(" WHERE t.account.id = :accid");
        q.append(" AND t.type = :type");
        q.append(" ORDER by t.transactionDate DESC");
        Query query = em.createQuery(q.toString());
        query.setParameter("accid", organizationId);
        query.setParameter("type", type);
        query.setFirstResult(firstResult);
        query.setMaxResults(listMax);
        resultList = query.getResultList();
        return resultList;
    } finally {
        log.debug("Closing entity manager .....");
        MyEMFDatabase.closeEntityManager(identity, MyEMFDatabase.MAIN_EM);
    }

}

From source file:es.ucm.fdi.dalgs.academicTerm.repository.AcademicTermRepository.java

@SuppressWarnings("unchecked")
public List<AcademicTerm> getAcademicsTerm(Integer pageIndex, Boolean showAll) {// String
    // term) {/*  w ww. j  a  v  a  2  s .  c  o m*/
    Query query = null;

    if (showAll)
        query = em.createQuery("select a from AcademicTerm a  order by a.term DESC");
    else
        query = em.createQuery("select a from AcademicTerm a  where a.isDeleted='false' order by a.term DESC");

    if (query.getResultList().isEmpty())
        return null;

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

}

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

/**
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 *///from www. j  a v  a  2 s  . c o  m
@SuppressWarnings(value = "unchecked")
public List<MessageContent> getSystemNotifications(int offset, int length) throws SocialSiteException {
    Query query = strategy.getNamedQuery("MessageContent.getSystemNotifications");
    query.setParameter(1, MessageContent.SYS_NOTIFICATIONS);
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return (List<MessageContent>) query.getResultList();
}

From source file:cn.guoyukun.spring.jpa.repository.RepositoryHelper.java

/**
 * ?/*from   w  w  w  .  ja  va2s.  c o  m*/
 *
 * @param ql
 * @param searchable
 * @param searchCallback
 * @return
 */
public <M> M findOne(final String ql, final Searchable searchable, final SearchCallback searchCallback) {

    assertConverted(searchable);

    StringBuilder s = new StringBuilder(ql);
    searchCallback.prepareQL(s, searchable);
    searchCallback.prepareOrder(s, searchable);
    Query query = getEntityManager().createQuery(s.toString());
    applyEnableQueryCache(query);
    searchCallback.setValues(query, searchable);
    searchCallback.setPageable(query, searchable);
    query.setMaxResults(1);
    List<M> result = query.getResultList();

    if (result.size() > 0) {
        return result.get(0);
    }
    return null;
}

From source file:org.codehaus.grepo.query.jpa.generator.QueryGeneratorBase.java

protected void applyMaxResultsSetting(GenericQuery genericQuery, QueryMethodParameterInfo qmpi,
        JpaQueryExecutionContext context, Query query) {
    Integer maxResults = GeneratorUtils.getMaxResults(qmpi, genericQuery, context.getMaxResults());
    if (maxResults != null) {
        query.setMaxResults(maxResults);
    }//  w ww . j a v  a2s  .  c  om
}

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

/**
 * {@inheritDoc}/*from www .  j a  v a 2  s.  c o m*/
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 */
@SuppressWarnings(value = "unchecked")
public List<PermissionGrant> getPermissionGrants(String gadgetDomain, int offset, int length)
        throws SocialSiteException {
    log.debug("gadgetDomain=" + gadgetDomain);
    Query query = strategy.getNamedQuery("PermissionGrant.getByGadgetDomain");
    query.setParameter(1, gadgetDomain);
    if (offset != 0)
        query.setFirstResult(offset);
    if (length != -1)
        query.setMaxResults(length);
    return (List<PermissionGrant>) query.getResultList();
}

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

/**
 * {@inheritDoc}//from w w w .  jav a 2s. c  o  m
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 */
@SuppressWarnings(value = "unchecked")
public List<PermissionGrant> getPermissionGrants(Profile profile, int offset, int length)
        throws SocialSiteException {
    log.debug("profile=" + profile);
    Query query = strategy.getNamedQuery("PermissionGrant.getByProfileId");
    query.setParameter(1, profile.getId());
    if (offset != 0)
        query.setFirstResult(offset);
    if (length != -1)
        query.setMaxResults(length);
    return (List<PermissionGrant>) query.getResultList();
}

From source file:org.medici.bia.dao.volume.VolumeDAOJpaImpl.java

/**
 * {@inheritDoc}//  ww  w  .  ja va  2s  . c  om
 */
@Override
public Volume findLastEntryVolume() throws PersistenceException {
    Query query = getEntityManager()
            .createQuery("FROM Volume WHERE logicalDelete = false ORDER BY dateCreated DESC");
    query.setMaxResults(1);

    return (Volume) query.getSingleResult();
}