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:com.uni.dao.etc.UniJpaRepository.java

/**
 * Reads the given {@link TypedQuery} into a {@link Page} applying the given {@link Pageable} and
 * {@link Specification}./*  w w w .j  a  v  a 2 s.  co m*/
 * 
 * @param query must not be {@literal null}.
 * @param spec can be {@literal null}.
 * @param pageable can be {@literal null}.
 * @return
 */
private Page<T> readPage(TypedQuery<T> query, Pageable pageable, Specification<T> spec) {

    query.setFirstResult(pageable.getOffset());
    query.setMaxResults(pageable.getPageSize());

    Long total = QueryUtils.executeCountQuery(getCountQuery(spec));
    List<T> content = total > pageable.getOffset() ? query.getResultList() : Collections.<T>emptyList();

    return new PageImpl<T>(content, pageable, total);
}

From source file:net.groupbuy.dao.impl.ProductDaoImpl.java

public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
    Root<Product> product = criteriaQuery.from(Product.class);
    Join<Product, OrderItem> orderItems = product.join("orderItems");
    Join<Product, net.groupbuy.entity.Order> order = orderItems.join("order");
    criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"),
            product.get("fullName"), product.get("price"),
            criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder
                    .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))));
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate));
    }//w  w w.j  a  v  a  2s .  com
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate));
    }
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid));
    criteriaQuery.where(restrictions);
    criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"),
            product.get("price"));
    criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(
            criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))));
    TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    if (count != null && count >= 0) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

From source file:com.dp2345.dao.impl.ProductDaoImpl.java

public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
    Root<Product> product = criteriaQuery.from(Product.class);
    Join<Product, OrderItem> orderItems = product.join("orderItems");
    Join<Product, com.dp2345.entity.Order> order = orderItems.join("order");
    criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"),
            product.get("fullName"), product.get("price"),
            criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder
                    .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))));
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate));
    }//from   w w  w  . java 2  s. c om
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate));
    }
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid));
    criteriaQuery.where(restrictions);
    criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"),
            product.get("price"));
    criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(
            criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))));
    TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    if (count != null && count >= 0) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

From source file:net.osxx.dao.impl.ProductDaoImpl.java

public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
    Root<Product> product = criteriaQuery.from(Product.class);
    Join<Product, OrderItem> orderItems = product.join("orderItems");
    Join<Product, net.osxx.entity.Order> order = orderItems.join("order");
    criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"),
            product.get("fullName"), product.get("price"),
            criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder
                    .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))));
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate));
    }/* ww w.j a  va  2  s. com*/
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate));
    }
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid));
    criteriaQuery.where(restrictions);
    criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"),
            product.get("price"));
    criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(
            criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))));
    TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    if (count != null && count >= 0) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

From source file:net.shopxx.dao.impl.ProductDaoImpl.java

public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
    Root<Product> product = criteriaQuery.from(Product.class);
    Join<Product, OrderItem> orderItems = product.join("orderItems");
    Join<Product, net.shopxx.entity.Order> order = orderItems.join("order");
    criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"),
            product.get("fullName"), product.get("price"),
            criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder
                    .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))));
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate));
    }//from  w ww  . j  av a  2 s .c o m
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate));
    }
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid));
    criteriaQuery.where(restrictions);
    criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"),
            product.get("price"));
    criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(
            criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))));
    TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    if (count != null && count >= 0) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

From source file:de.micromata.genome.db.jpa.genomecore.chronos.JpaJobStore.java

/**
 * Select jobs intern.//from   www .j  a  v  a  2s .com
 *
 * @param <T> the generic type
 * @param emgr the emgr
 * @param hostName the host name
 * @param jobName the job name
 * @param state the state
 * @param schedulerName the scheduler name
 * @param resultCount the result count
 * @param ret the ret
 * @param creator the creator
 * @return the list
 */
private <T extends TriggerJobDO> List<T> selectJobsIntern(DefaultEmgr emgr, String hostName, String jobName,
        String state, String schedulerName, int resultCount, List<T> ret, Createor<T> creator) {
    Long schedPk = null;
    SchedulerDAO scheddao = ChronosServiceManager.get().getSchedulerDAO();
    if (StringUtils.isNotBlank(schedulerName) == true) {
        schedPk = scheddao.getScheduler(schedulerName).getId();
    }
    String sql = "select  j from " + JpaTriggerJobDO.class.getSimpleName() + " j ";

    LogicClause clause = new LogicClause("and");
    if (state != null) {
        clause.getClauses().add(Clauses.equal("j.state", State.fromString(state)));
    } else {
        clause.getClauses().add(Clauses.notEqual("j.state", State.CLOSED));
    }
    if (schedPk != null) {
        clause.getClauses().add(Clauses.equal("j.scheduler", schedPk));
    }
    if (StringUtils.isNotBlank(hostName) == true) {
        clause.getClauses().add(Clauses.equal("j.hostName", hostName));
    }
    if (StringUtils.isNotBlank(jobName) == true) {
        clause.getClauses().add(Clauses.equal("j.jobName", jobName));
    }
    Map<String, Object> args = new HashMap<>();
    if (clause.getClauses().isEmpty() == false) {
        StringBuilder sb = new StringBuilder();
        clause.renderClause(sb, "", args);
        sql += " where " + sb.toString();
    }
    sql += " order by j.modifiedAt desc";

    TypedQuery<JpaTriggerJobDO> query = emgr.createQuery(JpaTriggerJobDO.class, sql, args);
    if (resultCount >= 1) {
        query.setMaxResults(resultCount);
    }
    List<JpaTriggerJobDO> resl = query.getResultList();

    for (JpaTriggerJobDO jt : resl) {
        T nr = creator.create();
        SchedJpaTypeConverter.fromDb(nr, jt);
        ret.add(nr);
    }
    return ret;
}

From source file:org.openmeetings.app.data.basic.Fieldmanagment.java

public List<Map<String, Object>> getLabelsByLanguage(Long language_id, int start, int max) {
    try {/*from www  .j  av  a  2 s .  c o  m*/

        String sql = "select f.fieldvalues.fieldvalues_id, f.value from Fieldlanguagesvalues as f "
                + "WHERE f.language_id = :language_id ";

        TypedQuery<Object> query = em.createQuery(sql, Object.class);
        query.setParameter("language_id", language_id);
        query.setFirstResult(start);
        query.setMaxResults(max);

        String appName = cfgManagement.getAppName();
        List<Map<String, Object>> returnList = new LinkedList<Map<String, Object>>();
        for (Object fl : query.getResultList()) {
            Map<String, Object> map = new HashMap<String, Object>();
            Object[] row = (Object[]) fl;
            map.put("id", (Long) row[0]);
            map.put("value", performReplace((String) row[1], appName));
            returnList.add(map);
        }
        FieldLanguage fieldLanguage = fieldLanguageDaoImpl.getFieldLanguageById(language_id);

        log.debug("Getting Labels for Language " + fieldLanguage.getName());

        // Check for Right To Left Languages
        if (fieldLanguage.getRtl()) {
            log.debug("Language requieres RTL!");

            List<Map<String, Object>> returnRtlList = new LinkedList<Map<String, Object>>();

            for (Iterator<Map<String, Object>> iter = returnList.iterator(); iter.hasNext();) {
                Map<String, Object> remote = iter.next();

                Map<String, Object> toAdd = new HashMap<String, Object>();
                toAdd.put("id", remote.get("id"));

                String value = remote.get("value").toString();

                String[] splitted = value.split(" ");
                String reverseOrder = "";
                for (int i = splitted.length - 1; i >= 0; i--) {
                    reverseOrder += splitted[i];
                    if (splitted.length != 1) {
                        reverseOrder += " ";
                    }
                }
                log.debug("reverseOrder " + reverseOrder);
                toAdd.put("value", value);

                returnRtlList.add(toAdd);
            }

            return returnRtlList;
        } else {
            log.debug("Language doesnt requiere RTL!");

            return returnList;

        }

    } catch (Exception ex2) {
        log.error("[getLabelsByLanguage]: ", ex2);
        ex2.printStackTrace();
    }
    return null;
}

From source file:org.terasoluna.tourreservation.domain.repository.tourinfo.TourInfoRepositoryImpl.java

protected List<TourInfo> findTourInfo(TourInfoSearchCriteria criteria, Pageable pageable) {

    String q = buildJpql(criteria.getTourDays(), criteria.getBasePrice());
    TypedQuery<TourInfo> query = entityManager.createQuery(q, TourInfo.class);

    query.setParameter("adultCount", criteria.getAdultCount());
    query.setParameter("childCount", criteria.getChildCount());
    query.setParameter("depDay", criteria.getDepDate());
    query.setParameter("depCode", criteria.getDepCode());
    query.setParameter("arrCode", criteria.getArrCode());
    if (criteria.getTourDays() != 0) {
        query.setParameter("tourDays", criteria.getTourDays());
    }//from   w  ww . j  a  va  2 s  . co m
    if (criteria.getBasePrice() != 0) {
        query.setParameter("basePrice", criteria.getBasePrice());
    }

    query.setFirstResult(pageable.getOffset());
    query.setMaxResults(pageable.getPageSize());

    List<TourInfo> tourInfoList = query.getResultList();
    return tourInfoList;
}

From source file:net.shopxx.dao.impl.ArticleCategoryDaoImpl.java

public List<ArticleCategory> findParents(ArticleCategory articleCategory, boolean recursive, Integer count) {
    if (articleCategory == null || articleCategory.getParent() == null) {
        return Collections.emptyList();
    }/*from w  w w . j  a  v a 2s. c om*/
    TypedQuery<ArticleCategory> query;
    if (recursive) {
        String jpql = "select articleCategory from ArticleCategory articleCategory where articleCategory.id in (:ids) order by articleCategory.grade asc";
        query = entityManager.createQuery(jpql, ArticleCategory.class).setParameter("ids",
                Arrays.asList(articleCategory.getParentIds()));
    } else {
        String jpql = "select articleCategory from ArticleCategory articleCategory where articleCategory = :articleCategory";
        query = entityManager.createQuery(jpql, ArticleCategory.class).setParameter("articleCategory",
                articleCategory.getParent());
    }
    if (count != null) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

From source file:net.shopxx.dao.impl.ProductCategoryDaoImpl.java

public List<ProductCategory> findParents(ProductCategory productCategory, boolean recursive, Integer count) {
    if (productCategory == null || productCategory.getParent() == null) {
        return Collections.emptyList();
    }/*  w  w w. ja  va2 s  . c o  m*/
    TypedQuery<ProductCategory> query;
    if (recursive) {
        String jpql = "select productCategory from ProductCategory productCategory where productCategory.id in (:ids) order by productCategory.grade asc";
        query = entityManager.createQuery(jpql, ProductCategory.class).setParameter("ids",
                Arrays.asList(productCategory.getParentIds()));
    } else {
        String jpql = "select productCategory from ProductCategory productCategory where productCategory = :productCategory";
        query = entityManager.createQuery(jpql, ProductCategory.class).setParameter("productCategory",
                productCategory.getParent());
    }
    if (count != null) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}