Example usage for javax.persistence TypedQuery setMaxResults

List of usage examples for javax.persistence TypedQuery setMaxResults

Introduction

In this page you can find the example usage for javax.persistence TypedQuery setMaxResults.

Prototype

TypedQuery<X> setMaxResults(int maxResult);

Source Link

Document

Set the maximum number of results to retrieve.

Usage

From source file:org.orcid.persistence.dao.impl.ProfileDaoImpl.java

@Override
public List<ProfileEntity> findProfilesThatMissedIndexing(int maxResults) {
    TypedQuery<ProfileEntity> query = entityManager.createQuery(
            "from ProfileEntity where (lastModified > lastIndexedDate or lastIndexedDate is null) and indexingStatus not in ('PENDING', 'IGNORE') order by lastModified",
            ProfileEntity.class);
    query.setMaxResults(maxResults);
    return query.getResultList();
}

From source file:org.seedstack.i18n.rest.internal.infrastructure.jpa.KeysQuery.java

@Override
public List<Key> getResultList(Range range) {
    if (!predicates.isEmpty()) {
        selectQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
    }//from   w ww.  j a va 2s.  c om

    // Get all the keys with their default translation
    TypedQuery<Key> query = entityManager.createQuery(selectQuery);
    if (range != null) {
        query.setFirstResult((int) range.getOffset());
        query.setMaxResults((int) range.getSize());
    }
    return query.getResultList();
}

From source file:org.seedstack.samples.store.infrastructure.jpa.JpaCategoryRepresentationFinder.java

@Override
protected List<CategoryRepresentation> computeResultList(Range range, Map<String, Object> criteria) {

    TypedQuery<CategoryRepresentation> query = entityManager.createQuery(
            "select new " + CategoryRepresentation.class.getName() + " (c.categoryId, c.name,c.urlImg) from "
                    + " Category c" + whereCategoryClause("c", criteria) + " order by c.categoryId",
            CategoryRepresentation.class);

    query.setFirstResult((int) range.getOffset());
    query.setMaxResults((int) range.getSize());
    return query.getResultList();
}

From source file:org.seedstack.samples.store.infrastructure.jpa.JpaProductRepresentationFinder.java

@Override
protected List<ProductRepresentation> computeResultList(Range range, Map<String, Object> criteria) {
    TypedQuery<ProductRepresentation> query = entityManager.createQuery("select new "
            + ProductRepresentation.class.getName()
            + "(p.entityId, p.designation, p.summary, p.details, p.picture, p.price,p.categoryId,cat.name)"
            + " from Product p,Category cat where p.categoryId=cat.categoryId " + getWhereClauseEnd()
            + " order by p.categoryId, p.entityId", ProductRepresentation.class);
    query.setFirstResult((int) range.getOffset());
    query.setMaxResults((int) range.getSize());
    return query.getResultList();
}

From source file:org.seedstack.showcase.infrastructure.finders.JpaCategoryfinder.java

@Override
protected List<CategoryRepresentation> computeResultList(Range range, Map<String, Object> criteria) {

    TypedQuery<CategoryRepresentation> query = entityManager.createQuery(
            "select new " + CategoryRepresentation.class.getName() + " (c.categoryId, c.name,c.urlImg) from "
                    + " Category c" + whereCategoryClause("c", criteria) + " order by c.categoryId",
            CategoryRepresentation.class);

    query.setFirstResult((int) range.getOffset());
    query.setMaxResults(range.getSize());
    return query.getResultList();
}

From source file:org.seedstack.showcase.infrastructure.finders.JpaProductRepresentationFinder.java

@Override
protected List<ProductRepresentation> computeResultList(Range range, Map<String, Object> criteria) {
    TypedQuery<ProductRepresentation> query = entityManager.createQuery("select new "
            + ProductRepresentation.class.getName()
            + "(p.entityId, p.designation, p.summary, p.details, p.picture, p.price,p.categoryId,cat.name)"
            + " from Product p,Category cat where p.categoryId=cat.categoryId " + getWhereClauseEnd()
            + " order by p.categoryId, p.entityId", ProductRepresentation.class);
    query.setFirstResult((int) range.getOffset());
    query.setMaxResults(range.getSize());
    return query.getResultList();
}

From source file:org.sigmah.server.dao.impl.FileHibernateDAO.java

/**
 * {@inheritDoc}// w ww  .j ava  2 s .com
 */
@Override
public FileVersion getLastVersion(Integer fileId) {

    final TypedQuery<FileVersion> query = em().createQuery(
            "SELECT fv FROM FileVersion fv WHERE fv.parentFile.id = :fileId ORDER BY fv.versionNumber DESC",
            FileVersion.class);

    query.setParameter("fileId", fileId);
    query.setMaxResults(1);

    return query.getSingleResult();
}

From source file:org.sofun.core.kup.KupServiceImpl.java

@Override
public KupSearchResults search(Map<String, String> params) throws CoreException {

    List<Kup> results = new ArrayList<Kup>();
    long count = 0;
    List<KupImpl> kups = null;

    int offset = 0;
    final String offsetStr = params.get("offset");
    if (offsetStr != null) {
        offset = Integer.valueOf(offsetStr);
    }//from   w w  w  .j  a  va 2  s  .c  o m
    int batchSize = 10; // default batch size
    final String batchSizeStr = params.get("batchSize");
    if (batchSizeStr != null) {
        batchSize = Integer.valueOf(batchSizeStr);
    }

    String queryStr = "";
    TypedQuery<KupImpl> query = null;
    Query countQuery = null;

    List<Byte> kupStatus = new ArrayList<Byte>();
    final String kupStatusParam = params.get("status");
    if (kupStatusParam == null) {
        kupStatus = null;
    } else if (kupStatusParam.equals("ALL")) {
        kupStatus = null;
    } else if (kupStatusParam.equals("OPENED")) {
        kupStatus.add(Integer.valueOf(1).byteValue()); // CREATED
    } else if (kupStatusParam.equals("ON_GOING")) {
        kupStatus.add(Integer.valueOf(2).byteValue()); // ON_GOING
    } else if (kupStatusParam.equals("ALL_OPENED")) {
        kupStatus.add(Integer.valueOf(1).byteValue()); // CREATED
        kupStatus.add(Integer.valueOf(2).byteValue()); // ON_GOING
    } else if (kupStatusParam.equals("ALL_CLOSED")) {
        kupStatus.add(Integer.valueOf(3).byteValue()); // CLOSED
        kupStatus.add(Integer.valueOf(4).byteValue()); // SETTLED
        kupStatus.add(Integer.valueOf(5).byteValue()); // PAID OUT
        kupStatus.add(Integer.valueOf(4).byteValue()); // SETTLED
        kupStatus.add(Integer.valueOf(-1).byteValue()); // CANCELED
    }

    final String email = params.get("email");
    if (email != null) {

        Member member = members.getMember(email);
        if (member != null) {

            String template = params.get("template");
            if (template != null && template.equals("all")) {

                queryStr = "SELECT k FROM " + KupImpl.class.getSimpleName()
                        + " k JOIN k.members m WHERE m.id=:member_id";
                if (kupStatus != null) {
                    queryStr += " AND k.status IN (:status)";
                }
                queryStr += " ORDER BY k.created";

                query = em.createQuery(queryStr, KupImpl.class);
                countQuery = em.createQuery(
                        "SELECT count(*) "
                                + queryStr.substring(queryStr.indexOf("FROM"), queryStr.indexOf("ORDER BY")),
                        Long.class);
                query.setParameter("member_id", member.getId());
                countQuery.setParameter("member_id", member.getId());
                if (kupStatus != null) {
                    query.setParameter("status", kupStatus);
                    countQuery.setParameter("status", kupStatus);
                }

            } else {

                queryStr = "SELECT k FROM " + KupImpl.class.getSimpleName()
                        + " k JOIN k.members m WHERE m.id=:member_id AND k.isTemplate=:isTemplate";
                if (kupStatus != null && kupStatus.size() > 0) {
                    queryStr += " AND k.status IN (:status)";
                }
                queryStr += " ORDER BY k.created";

                query = em.createQuery(queryStr, KupImpl.class);
                countQuery = em.createQuery(
                        "SELECT count(*) "
                                + queryStr.substring(queryStr.indexOf("FROM"), queryStr.indexOf("ORDER BY")),
                        Long.class);

                query.setParameter("member_id", member.getId());
                countQuery.setParameter("member_id", member.getId());

                query.setParameter("isTemplate", true);
                countQuery.setParameter("isTemplate", true);
                if (kupStatus != null && kupStatus.size() > 0) {
                    query.setParameter("status", kupStatus);
                    countQuery.setParameter("status", kupStatus);
                }

            }

            // pagination
            query.setFirstResult(offset);
            query.setMaxResults(batchSize);

            kups = query.getResultList();
            count = (Long) countQuery.getSingleResult();
            if (kups != null) {
                results.addAll(kups);
            }

        }

    } else {

        if (kupStatusParam == null || kupStatusParam.isEmpty()) {
            return null;
        }

        boolean includeRoomKups = false;
        final String withRoomKups = params.get("withRoomKups");
        if (withRoomKups != null && "1".equals(withRoomKups)) {
            includeRoomKups = true;
        }

        if (!includeRoomKups) {
            queryStr = "from " + KupImpl.class.getSimpleName() + " k where k.isTemplate=:isTemplate";
        } else {
            queryStr = "from " + KupImpl.class.getSimpleName() + " k where k.team.privacy IN (:teamPrivacy)";
        }
        if (kupStatus != null) {
            queryStr += " AND k.status IN (:status)";
        }
        final String name = params.get("name");
        if (name != null && !"".equals(name)) {
            queryStr += " AND k.name IN (:name)";
        }

        boolean isTemplateParam = true;
        final String isTemplate = params.get("isTemplate");
        if (isTemplate != null && !"".equals(isTemplate)) {
            if (!isTemplate.equals("1")) {
                isTemplateParam = false;
            }
        }

        final String kupStakeParam = params.get("stake");
        if (kupStakeParam != null) {
            if ("FREE_FREEROLL".equals(kupStakeParam)) {
                queryStr += " AND k.stake=0";
            } else if ("FREEROLL".equals(kupStakeParam)) {
                queryStr += " AND k.stake=0 AND k.type='GAMBLING_FR'";
            } else if ("GAMBLING".equals(kupStakeParam)) {
                queryStr += " AND k.type='GAMBLING_FR' AND k.stake>0";
            } else if ("FREE".equals(kupStakeParam)) {
                queryStr += " AND k.type='FREE'";
            } else if ("ALL_GAMBLING".equals(kupStakeParam)) {
                queryStr += " AND k.type='GAMBLING_FR'";
            } else if (kupStakeParam.isEmpty()) {
                return null;
            }
        }

        final String kupSportsParam = params.get("sports");
        List<String> sportsNameParams = null;
        if (kupSportsParam != null) {
            if (kupSportsParam.isEmpty()) {
                return null;
            }
            List<String> sparams = Arrays.asList(kupSportsParam.split("#"));
            if (!sparams.contains("ALL")) {
                queryStr += " AND UPPER(k.sport.name) IN (:sports)";
                sportsNameParams = sparams;
            }

        }

        // Remove kups where player is a participant
        final String removeValidatedFor = params.get("removeValidatedfor");
        Member removeValidatedMember = null;
        if (removeValidatedFor != null && !removeValidatedFor.isEmpty()) {
            queryStr += " AND :member NOT MEMBER OF k.participants";
            removeValidatedMember = members.getMember(removeValidatedFor);
        }

        // Do not show up with no participants
        if (kupStatusParam != null && "ALL_CLOSED".equals(kupStatusParam)) {
            queryStr += " AND k.nbParticipants > 0";
        }

        // Sorting
        final String sortParams = params.get("sort");
        if (sortParams != null) {
            if (sortParams.isEmpty()) {
                return null;
            }
            queryStr += " ORDER BY";
            List<String> sparams = Arrays.asList(sortParams.split("#"));
            boolean initialized = false;
            if (sparams.contains("START_DATE")) {
                if ("ALL_CLOSED".equals(kupStatusParam)) {
                    queryStr += " k.endDate DESC";
                } else {
                    queryStr += " k.status ASC, k.startDate ASC";
                }
                initialized = true;
            }
            if (sparams.contains("JACKPOT")) {
                if (initialized) {
                    queryStr += " ,";
                }
                queryStr += " k.guaranteedPrice DESC";
            }
            if (sparams.contains("PARTICIPANTS")) {
                if (initialized) {
                    queryStr += " ,";
                }
                queryStr += " k.nbParticipants DESC";
            }
            if (sparams.contains("KUP_DURATION")) {
                if (initialized) {
                    queryStr += " ,";
                }
                queryStr += " k.duration DESC";
            }
        } else {
            if (kupStatusParam != null && "ALL_CLOSED".equals(kupStatusParam)) {
                queryStr += " ORDER BY k.endDate DESC";
            } else {
                queryStr += " ORDER BY k.status ASC, k.startDate ASC";
            }
        }

        query = em.createQuery(queryStr, KupImpl.class);
        countQuery = em.createQuery("SELECT count(*) " + queryStr.substring(0, queryStr.indexOf("ORDER BY")),
                Long.class);

        if (!includeRoomKups) {
            query.setParameter("isTemplate", isTemplateParam);
            countQuery.setParameter("isTemplate", isTemplateParam);
        } else {
            String[] teamPrivacy = new String[] { TeamPrivacy.PUBLIC, TeamPrivacy.PUBLIC_GAMBLING_FR };
            query.setParameter("teamPrivacy", Arrays.asList(teamPrivacy));
            countQuery.setParameter("teamPrivacy", Arrays.asList(teamPrivacy));
        }
        if (kupStatus != null) {
            query.setParameter("status", kupStatus);
            countQuery.setParameter("status", kupStatus);
        }
        if (sportsNameParams != null) {
            query.setParameter("sports", sportsNameParams);
            countQuery.setParameter("sports", sportsNameParams);
        }
        if (name != null && !"".equals(name)) {
            final String[] names = name.split(",");
            query.setParameter("name", Arrays.asList(names));
            countQuery.setParameter("name", Arrays.asList(names));
        }
        if (removeValidatedFor != null && !removeValidatedFor.isEmpty()) {
            query.setParameter("member", removeValidatedMember);
            countQuery.setParameter("member", removeValidatedMember);
        }

        // pagination
        query.setFirstResult(offset);
        query.setMaxResults(batchSize);

        kups = query.getResultList();
        count = (Long) countQuery.getSingleResult();
        if (kups != null) {
            results.addAll(kups);
        }

    }

    return new KupSearchResultsImpl(offset, batchSize, count, results);

}

From source file:org.sparkcommerce.core.catalog.dao.ProductDaoImpl.java

public List<Product> readActiveProductsByCategoryInternal(Long categoryId, Date currentDate, int limit,
        int offset) {
    TypedQuery<Product> query = em.createNamedQuery("BC_READ_ACTIVE_PRODUCTS_BY_CATEGORY", Product.class);
    query.setParameter("categoryId", sandBoxHelper.mergeCloneIds(em, CategoryImpl.class, categoryId));
    query.setParameter("currentDate", currentDate);
    query.setFirstResult(offset);//  w w w. j  av a 2  s. c  o m
    query.setMaxResults(limit);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");

    return query.getResultList();
}

From source file:org.sparkcommerce.core.catalog.dao.ProductDaoImpl.java

@Override
public List<Product> readProductsByCategory(Long categoryId, int limit, int offset) {
    TypedQuery<Product> query = em.createNamedQuery("BC_READ_PRODUCTS_BY_CATEGORY", Product.class);
    query.setParameter("categoryId", sandBoxHelper.mergeCloneIds(em, CategoryImpl.class, categoryId));
    query.setFirstResult(offset);//from ww  w .j ava  2 s. com
    query.setMaxResults(limit);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");

    return query.getResultList();
}