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.openmeetings.app.data.user.Organisationmanagement.java

/**
 * //  w ww. j a  v a  2 s . c om
 * @param user_level
 * @return
 */
public List<Organisation> getOrganisations(int start, int max, String orderby, boolean asc) {
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Organisation> cq = cb.createQuery(Organisation.class);
        Root<Organisation> c = cq.from(Organisation.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        cq.where(condition);
        cq.distinct(asc);
        if (asc) {
            cq.orderBy(cb.asc(c.get(orderby)));
        } else {
            cq.orderBy(cb.desc(c.get(orderby)));
        }
        TypedQuery<Organisation> q = em.createQuery(cq);
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<Organisation> ll = q.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("[getOrganisations]", ex2);
    }
    return null;
}

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

/**
 * //from   w ww. ja  v a2  s .  c  om
 * list entity by criteriaInfo
 * 
 * @param criteriaInfo
 * @return 
 */
public PagedResult<T> findAll(CriteriaInfo criteriaInfo) {
    EntityManager em = getEntityManager();

    ParamInfo paramInfo = criteriaInfo.getParamInfo();
    PageInfo pageInfo = criteriaInfo.getPageInfo();
    SortInfo sortInfo = criteriaInfo.getSortInfo();

    StringBuilder from = new StringBuilder();
    from.append(String.format("%s t ", entityClass.getSimpleName()));

    StringBuilder where = new StringBuilder();
    Map<String, Object> paramMap = new HashMap<>();

    where = prepareWhereCondition(paramInfo, paramMap, where);

    StringBuilder orderBy = new StringBuilder(" ");
    if (sortInfo != null) {
        Iterator<SortItem> iterator = sortInfo.getSortItems().iterator();
        while (iterator.hasNext()) {
            SortItem item = iterator.next();
            orderBy.append("t.").append(item.getFieldName()).append(" ")
                    .append(item.isDescending() ? "desc" : "asc").append(",");
        }
        orderBy = new StringBuilder(orderBy.substring(0, orderBy.length() - 1));
        ;
    }

    String query = toSql(where, from, orderBy, false);
    String countquery = toSql(where, from, orderBy, true);

    TypedQuery<T> cq = em.createQuery(query, entityClass);
    //??
    TypedQuery<Long> countq = em.createQuery(countquery, Long.class);

    for (String key : paramMap.keySet()) {
        Object value = paramMap.get(key);
        cq.setParameter(key, value);
        countq.setParameter(key, value);
    }

    Long countResult = countq.getSingleResult();

    if (pageInfo != null) {
        cq.setFirstResult(pageInfo.getOffset());
        cq.setMaxResults(pageInfo.getSize());
    }

    return new PagedResult(cq.getResultList(), countResult == null ? 0 : countResult.intValue());
}

From source file:com.ushahidi.swiftriver.core.api.dao.impl.JpaDropDao.java

public List<Drop> findAll(long sinceId, int batchSize) {
    String qlString = "FROM Drop d WHERE d.id > :sinceId ORDER BY d.id ASC";
    TypedQuery<Drop> query = em.createQuery(qlString, Drop.class);
    query.setParameter("sinceId", sinceId);
    query.setMaxResults(batchSize);

    return query.getResultList();
}

From source file:gov.guilin.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, gov.guilin.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  ww  .j a v a2s . 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:org.openmeetings.app.data.user.dao.PrivateMessagesDaoImpl.java

public List<PrivateMessages> getTrashPrivateMessagesByUser(Long user_id, String search, String orderBy,
        int start, Boolean asc, int max) {
    try {/*  w  ww. j a va 2s. c  o m*/

        String hql = "select c from PrivateMessages c " + "where c.isTrash = true "
                + "AND c.owner.user_id = :user_id ";

        if (search.length() != 0) {
            hql += "AND ( ";
            hql += "lower(c.subject) LIKE :search ";
            hql += "OR lower(c.message) LIKE :search ";
            hql += "OR lower(c.from.firstname) LIKE :search ";
            hql += "OR lower(c.from.lastname) LIKE :search ";
            hql += "OR lower(c.from.login) LIKE :search ";
            hql += "OR lower(c.from.adresses.email) LIKE :search ";
            hql += " ) ";
        }

        hql += "ORDER BY " + orderBy;

        if (asc) {
            hql += " ASC";
        } else {
            hql += " DESC";
        }

        TypedQuery<PrivateMessages> query = em.createQuery(hql, PrivateMessages.class);
        if (search.length() != 0) {
            query.setParameter("search", StringUtils.lowerCase("%" + search + "%"));
        }
        query.setParameter("user_id", user_id);
        query.setFirstResult(start);
        query.setMaxResults(max);
        List<PrivateMessages> ll = query.getResultList();

        return ll;
    } catch (Exception e) {
        log.error("[getTrashPrivateMessagesByUser]", e);
    }
    return null;
}

From source file:org.openmeetings.app.data.user.Organisationmanagement.java

/**
 * get a list of all users of an organisation
 * //from  w ww .  j a va 2s  . c o m
 * @param user_level
 * @param organisation_id
 * @param start
 * @param max
 * @param orderby
 * @param asc
 * @return
 */
public List<Users> getUsersByOrganisationId(long organisation_id, int start, int max, String orderby,
        boolean asc) {
    try {
        String hql = "SELECT c FROM " + "Users c " + ", IN(c.organisation_users) ou "
                + "WHERE c.deleted = 'false' AND ou.organisation.organisation_id = :organisation_id ";
        if (orderby.startsWith("c.")) {
            hql += "ORDER BY " + orderby;
        } else {
            hql += "ORDER BY " + "c." + orderby;
        }
        if (asc) {
            hql += " ASC";
        } else {
            hql += " DESC";
        }

        TypedQuery<Users> q = em.createQuery(hql, Users.class);
        q.setParameter("organisation_id", organisation_id);
        q.setFirstResult(start);
        q.setMaxResults(max);

        List<Users> userL = q.getResultList();
        return userL;
    } catch (Exception ex2) {
        log.error("[getUsersByOrganisationId]", ex2);
    }
    return null;
}

From source file:org.openmeetings.app.data.user.Organisationmanagement.java

/**
 * //from w  ww.j  av a2 s.c  o m
 * @param user_level
 * @param user_id
 * @param start
 * @param max
 * @param orderby
 * @param asc
 * @return
 */
public List<Organisation> getRestOrganisationsByUserId(long user_level, long user_id, int start, int max,
        String orderby, boolean asc) {
    try {
        if (authLevelManagement.checkAdminLevel(user_level)) {
            String qSQL = "SELECT o FROM Organisation AS o " + "WHERE o.organisation_id NOT IN ("
                    + "   SELECT ou.organisation.organisation_id "
                    + "   FROM Users u, IN(u.organisation_users) ou WHERE u.deleted = 'false' AND u.user_id = :user_id)";
            TypedQuery<Organisation> q = em.createQuery(qSQL, Organisation.class);
            q.setParameter("user_id", user_id);
            q.setFirstResult(start);
            q.setMaxResults(max);
            return q.getResultList();
        }
    } catch (Exception ex2) {
        log.error("[getRestOrganisationsByUserId]", ex2);
    }
    return null;
}

From source file:com.scipub.dao.jpa.Dao.java

protected <T> List<T> findByQuery(QueryDetails<T> details) {
    if (details.getResultClass() == null) {
        throw new IllegalArgumentException("You must specify a result class");
    }//from w  ww .j av  a 2s .c o  m
    TypedQuery<T> q = null;
    if (details.getQueryName() != null) {
        q = entityManager.createNamedQuery(details.getQueryName(), details.getResultClass());
    } else if (details.getQuery() != null) {
        q = entityManager.createQuery(details.getQuery(), details.getResultClass());
    } else {
        throw new IllegalArgumentException("Either query or query name must be set");
    }

    for (int i = 0; i < details.getParamNames().length; i++) {
        q.setParameter(details.getParamNames()[i], details.getParamValues()[i]);
    }
    if (details.getStart() > -1) {
        q.setFirstResult(details.getStart());
    }
    if (details.getCount() > -1) {
        q.setMaxResults(details.getCount());
    }
    if (details.isCacheable()) {
        setCacheable(q);
    }
    return q.getResultList();
}

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

public List<ArticleCategory> findChildren(ArticleCategory articleCategory, Integer count) {
    TypedQuery<ArticleCategory> query;
    if (articleCategory != null) {
        String jpql = "select articleCategory from ArticleCategory articleCategory where articleCategory.treePath like :treePath order by articleCategory.order asc";
        query = entityManager.createQuery(jpql, ArticleCategory.class).setFlushMode(FlushModeType.COMMIT)
                .setParameter("treePath", "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId()
                        + ArticleCategory.TREE_PATH_SEPARATOR + "%");
    } else {/*w w w  .  ja v a 2 s.c  o  m*/
        String jpql = "select articleCategory from ArticleCategory articleCategory order by articleCategory.order asc";
        query = entityManager.createQuery(jpql, ArticleCategory.class).setFlushMode(FlushModeType.COMMIT);
    }
    if (count != null) {
        query.setMaxResults(count);
    }
    return sort(query.getResultList(), articleCategory);
}

From source file:org.openmeetings.app.data.user.Organisationmanagement.java

/**
 * Filter all Organisations by user TODO: Add sorting
 * /*from www  .j  a va  2  s. c  o  m*/
 * @param user_level
 * @param users_id
 * @param start
 * @param max
 * @param orderby
 * @return
 */
public List<Organisation> getOrganisationsByUserId(long user_level, long user_id, int start, int max,
        String orderby, boolean asc) {
    try {
        if (authLevelManagement.checkAdminLevel(user_level)) {
            TypedQuery<Organisation> q = em.createNamedQuery("getOrganisationsByUserId", Organisation.class);
            q.setParameter("user_id", user_id);
            q.setFirstResult(start);
            q.setMaxResults(max);

            List<Organisation> userOrg = q.getResultList();

            return userOrg;
        }
    } catch (Exception ex2) {
        log.error("[getOrganisationsByUserId]", ex2);
    }
    return null;
}