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:net.groupbuy.dao.impl.ProductCategoryDaoImpl.java

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

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

public List<ArticleCategory> findChildren(ArticleCategory articleCategory, boolean recursive, Integer count) {
    TypedQuery<ArticleCategory> query;
    if (recursive) {
        if (articleCategory != null) {
            String jpql = "select articleCategory from ArticleCategory articleCategory where articleCategory.treePath like :treePath order by articleCategory.grade asc, articleCategory.order asc";
            query = entityManager.createQuery(jpql, ArticleCategory.class).setParameter("treePath",
                    "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId()
                            + ArticleCategory.TREE_PATH_SEPARATOR + "%");
        } else {//  www .jav a  2 s .  c  o  m
            String jpql = "select articleCategory from ArticleCategory articleCategory order by articleCategory.grade asc, articleCategory.order asc";
            query = entityManager.createQuery(jpql, ArticleCategory.class);
        }
        if (count != null) {
            query.setMaxResults(count);
        }
        List<ArticleCategory> result = query.getResultList();
        sort(result);
        return result;
    } else {
        String jpql = "select articleCategory from ArticleCategory articleCategory where articleCategory.parent = :parent order by articleCategory.order asc";
        query = entityManager.createQuery(jpql, ArticleCategory.class).setParameter("parent", articleCategory);
        if (count != null) {
            query.setMaxResults(count);
        }
        return query.getResultList();
    }
}

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

public List<ProductCategory> findChildren(ProductCategory productCategory, boolean recursive, Integer count) {
    TypedQuery<ProductCategory> query;
    if (recursive) {
        if (productCategory != null) {
            String jpql = "select productCategory from ProductCategory productCategory where productCategory.treePath like :treePath order by productCategory.grade asc, productCategory.order asc";
            query = entityManager.createQuery(jpql, ProductCategory.class).setParameter("treePath",
                    "%" + ProductCategory.TREE_PATH_SEPARATOR + productCategory.getId()
                            + ProductCategory.TREE_PATH_SEPARATOR + "%");
        } else {/*from   w w  w .  j  a va2 s. c  o  m*/
            String jpql = "select productCategory from ProductCategory productCategory order by productCategory.grade asc, productCategory.order asc";
            query = entityManager.createQuery(jpql, ProductCategory.class);
        }
        if (count != null) {
            query.setMaxResults(count);
        }
        List<ProductCategory> result = query.getResultList();
        sort(result);
        return result;
    } else {
        String jpql = "select productCategory from ProductCategory productCategory where productCategory.parent = :parent order by productCategory.order asc";
        query = entityManager.createQuery(jpql, ProductCategory.class).setParameter("parent", productCategory);
        if (count != null) {
            query.setMaxResults(count);
        }
        return query.getResultList();
    }
}

From source file:org.openmeetings.app.data.user.dao.PrivateMessagesDaoImpl.java

public List<PrivateMessages> getSendPrivateMessagesByUser(Long toUserId, String search, String orderBy,
        int start, Boolean asc, Long privateMessageFolderId, int max) {
    try {/* ww w  .ja va 2  s. c o m*/

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

        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);
        query.setParameter("toUserId", toUserId);
        query.setParameter("privateMessageFolderId", privateMessageFolderId);
        if (search.length() != 0) {
            query.setParameter("search", StringUtils.lowerCase("%" + search + "%"));
        }
        query.setFirstResult(start);
        query.setMaxResults(max);
        List<PrivateMessages> ll = query.getResultList();

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

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

private List<Fieldvalues> getFieldsValues(int start, int max, String orderby, boolean asc, String search)
        throws Exception {
    String queryLanguage = "select c from Fieldvalues c where c.deleted = 'false'";
    if (search.length() > 0) {
        queryLanguage += " AND (c.name LIKE :searchStr " + "OR c.fieldvalues_id = :fieldvalues_id)";
    }/*from   w  w  w.  ja v  a 2s .c o m*/
    if (asc) {
        queryLanguage += " ORDER BY " + orderby + " ASC";
    } else {
        queryLanguage += " ORDER BY " + orderby + "DESC";
    }
    TypedQuery<Fieldvalues> q = em.createQuery(queryLanguage, Fieldvalues.class);
    if (search.length() > 0) {
        q.setParameter("searchStr", "%" + search + "%");
        long idNumber = 0;
        try {
            idNumber = Long.parseLong(search);
        } catch (NumberFormatException nfe) {
        }
        q.setParameter("fieldvalues_id", idNumber);
    }
    q.setFirstResult(start);
    q.setMaxResults(max);
    return q.getResultList();
}

From source file:edu.purdue.cybercenter.dm.service.VocabularyService.java

public edu.purdue.cybercenter.dm.domain.Vocabulary findByUuidAndVersion(UUID uuid, UUID version) {
    TypedQuery<edu.purdue.cybercenter.dm.domain.Vocabulary> query;
    edu.purdue.cybercenter.dm.domain.Vocabulary dbVocabulary;
    if (version != null) {
        query = DomainObjectHelper.createNamedQuery("Vocabulary.findByUuidAndVersionNumber",
                edu.purdue.cybercenter.dm.domain.Vocabulary.class);
        query.setParameter("versionNumber", version);
    } else {/*w  ww .j  ava2 s  . c  o m*/
        query = DomainObjectHelper.createNamedQuery("Vocabulary.findByUuid",
                edu.purdue.cybercenter.dm.domain.Vocabulary.class);
    }

    query.setParameter("uuid", uuid);
    query.setMaxResults(1);
    try {
        dbVocabulary = domainObjectService.executeTypedQueryWithSingleResult(query);
    } catch (EmptyResultDataAccessException ex) {
        dbVocabulary = null;
    }

    return dbVocabulary;
}

From source file:org.openmeetings.app.data.user.dao.PrivateMessagesDaoImpl.java

public List<PrivateMessages> getPrivateMessagesByUser(Long toUserId, String search, String orderBy, int start,
        Boolean asc, Long privateMessageFolderId, int max) {
    try {//ww w  .  jav  a 2s .co m

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

        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);
        query.setParameter("toUserId", toUserId);
        query.setParameter("isTrash", false);
        query.setParameter("privateMessageFolderId", privateMessageFolderId);
        if (search.length() != 0) {
            query.setParameter("search", StringUtils.lowerCase("%" + search + "%"));
        }
        query.setFirstResult(start);
        query.setMaxResults(max);
        List<PrivateMessages> ll = query.getResultList();

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

From source file:org.openmeetings.app.data.user.dao.PrivateMessagesDaoImpl.java

public List<PrivateMessages> getFolderPrivateMessagesByUser(Long toUserId, String search, String orderBy,
        int start, Boolean asc, Long privateMessageFolderId, int max) {
    try {//from  w w  w.ja v a 2 s .com

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

        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";
        }

        log.debug("HQL " + hql);

        log.debug("privateMessageFolderId " + privateMessageFolderId);

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

        return ll;

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

From source file:edu.kit.dama.mdm.core.jpa.MetaDataManagerJpa.java

@Override
public final <T> List<T> findResultList(String queryString, Object[] pParameters, Class<T> entityClass,
        int pFirstIdx, int pMaxResults) throws UnauthorizedAccessAttemptException {
    List<T> result = new ArrayList();
    try {/* w w  w .ja  v  a 2  s  .  c  o  m*/
        TypedQuery<T> q = entityManager.createQuery(queryString, entityClass);
        applyProperties(q);
        if (pParameters != null && pParameters.length != 0) {
            for (int i = 0; i < pParameters.length; i++) {
                q.setParameter(i + 1, pParameters[i]);
            }
        }
        q.setFirstResult((pFirstIdx >= 0) ? pFirstIdx : 0);
        if (pMaxResults > 0) {
            q.setMaxResults(pMaxResults);
        }
        result = q.getResultList();
    } catch (RuntimeException re) {
        LOGGER.warn("Failed to obtain typed query result list", re);
    } finally {
        finalizeEntityManagerAccess("find result list with plain SQL '" + queryString + "'", null, entityClass);
    }
    return result;
}