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.epam.ipodromproject.repository.jpa.JPAHorseRepository.java

@Override
public List<Horse> getHorsesByPageAndPartOfName(String partOfName, int startingResult, int resultsCount) {
    TypedQuery<Horse> query = entityManager.createNamedQuery("Horse.getHorsesByPageAndPartOfName", Horse.class);
    StringBuilder partOfNameBuilder = new StringBuilder("%");
    query.setParameter("partOfName", partOfNameBuilder.append(partOfName).append("%").toString());
    return query.setMaxResults(resultsCount).setFirstResult(startingResult).getResultList();
}

From source file:de.taimos.dao.hibernate.EntityDAOHibernate.java

@Override
public List<E> findList(final int first, final int max) {
    final TypedQuery<E> query = this.entityManager.createQuery(this.getFindListQuery(), this.getEntityClass());
    if (first >= 0) {
        query.setFirstResult(first);//from  w w w  .j  ava 2s.  c  om
    }
    if (max >= 0) {
        query.setMaxResults(max);
    }
    return query.getResultList();
}

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

public List<ArticleCategory> findParents(ArticleCategory articleCategory, Integer count) {
    if (articleCategory == null || articleCategory.getParent() == null) {
        return Collections.<ArticleCategory>emptyList();
    }/*from w w  w. j av a2s. c  o m*/
    String jpql = "select articleCategory from ArticleCategory articleCategory where articleCategory.id in (:ids) order by articleCategory.grade asc";
    TypedQuery<ArticleCategory> query = entityManager.createQuery(jpql, ArticleCategory.class)
            .setFlushMode(FlushModeType.COMMIT).setParameter("ids", articleCategory.getTreePaths());
    if (count != null) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

From source file:ru.portal.services.TableServiceImpl.java

@Override
public Page<HashMap<String, String>> findAll(String tableOrViewName, Pageable pageable) {

    List<HashMap<String, String>> result = new ArrayList<>();

    EntityType<?> type = null;/*from ww  w  .j  a v a 2 s. c  o  m*/
    Set<EntityType<?>> set = em.getEntityManagerFactory().getMetamodel().getEntities();
    for (EntityType<?> entityType : set) {
        if (entityType.getBindableJavaType().getAnnotation(PortalTable.class) != null) {
            if (entityType.getBindableJavaType().getName().equals(tableOrViewName)) {
                type = entityType;
                break;
            }
        }
    }

    Long totalRows = 0L;

    if (type != null) {
        Class<?> bindableJavaType = type.getBindableJavaType();

        //count
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
        countQuery.select(criteriaBuilder.count(countQuery.from(bindableJavaType)));
        totalRows = em.createQuery(countQuery).getSingleResult();

        //select
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<?> cq = cb.createQuery(bindableJavaType);
        Root<?> root = cq.from(bindableJavaType);
        //          cq.select(root);
        if (pageable == null) {
            pageable = new PageRequest(0, 50);
        }

        TypedQuery<?> query = em.createQuery(cq);

        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<?> all = query.getResultList();

        List<String> columns = getTableOrViewMetaData(tableOrViewName);

        for (Object object : all) {

            HashMap<String, String> res = new HashMap<>(columns.size());
            Class<? extends Object> clazz = object.getClass();
            for (String fieldName : columns) {
                try {
                    Field field = clazz.getDeclaredField(fieldName);
                    field.setAccessible(true);
                    Object fieldValue = field.get(object);
                    res.put(fieldName, fieldValue.toString());
                    //TODO cast data integer long etc
                } catch (NoSuchFieldException | SecurityException | IllegalArgumentException
                        | IllegalAccessException ex) {
                    Logger.getLogger(TableServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            result.add(res);
        }
    }

    PageImpl<HashMap<String, String>> list = new PageImpl<>(result, pageable, totalRows);
    return list;
}

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

public List<ProductCategory> findParents(ProductCategory productCategory, Integer count) {
    if (productCategory == null || productCategory.getParent() == null) {
        return Collections.<ProductCategory>emptyList();
    }//w  w  w . j av  a 2s . co  m
    String jpql = "select productCategory from ProductCategory productCategory where productCategory.id in (:ids) order by productCategory.grade asc";
    TypedQuery<ProductCategory> query = entityManager.createQuery(jpql, ProductCategory.class)
            .setFlushMode(FlushModeType.COMMIT).setParameter("ids", productCategory.getTreePaths());
    if (count != null) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

From source file:com.pingdu.dao.licenseDao.LicenseDao.java

public List<License> searchLicenses(String searchType, Object keyWord, int page, int rows) {
    String jpql = new String();
    int head = (page - 1) * rows;
    if (searchType.equals("1")) {
        jpql = "select lic from License lic where lic.entCode =:keyWord order by lic.licenseCode";
    }/*  w  ww .jav a  2 s.  c  om*/
    if (searchType.equals("???")) {
        jpql = "select lic from License lic where lic.licTypeCode =:keyWord order by lic.licenseCode";
    }
    if (searchType.equals("")) {
        jpql = "select lic from License lic where lic.uploadDate =:keyWord order by lic.licenseCode";
    }
    if (searchType.equals("")) {
        jpql = "select lic from License lic where lic.expireDate =:keyWord order by lic.licenseCode";
    }
    if (searchType.equals("?")) {
        jpql = "select lic from License lic where lic.isExpire =:keyWord order by lic.licenseCode";
    }
    TypedQuery<License> query = em().createQuery(jpql, License.class);
    //query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
    query.setParameter("keyWord", keyWord);
    query.setFirstResult(head);
    query.setMaxResults(rows);
    List<License> list = query.getResultList();
    return list;
}

From source file:edu.sabanciuniv.sentilab.sare.controllers.entitymanagers.LexiconBuilderController.java

/**
 * Gets the document at the given rank.// w w  w  .  ja  v  a  2 s .co  m
 * @param em the {@link EntityManager} to use.
 * @param builder the {@link LexiconBuilderDocumentStore} to use.
 * @param rank the rank of the document. If {@code null}, this returns the same result as {@code getNextDocument}.
 * @return the {@link LexiconBuilderDocument} at the given rank.
 */
public LexiconBuilderDocument getDocument(EntityManager em, LexiconBuilderDocumentStore builder, Long rank) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");
    Validate.notNull(builder, CannedMessages.NULL_ARGUMENT, "builder");

    if (rank == null) {
        return this.getNextDocument(em, builder);
    }

    TypedQuery<LexiconBuilderDocument> query = this.getDocumentsQuery(em, builder, null);
    query.setFirstResult(rank.intValue());
    query.setMaxResults(1);
    LexiconBuilderDocument doc = this.getSingleResult(query);
    doc.setRank(rank);
    return doc;
}

From source file:org.jasig.portlet.notice.service.jpa.JpaNotificationDao.java

@Override
@Transactional(readOnly = true)/*  ww w  . j a  v  a 2  s .  co m*/
public List<JpaEntry> list(Integer page, Integer pageSize) {
    TypedQuery<JpaEntry> query = entityManager.createNamedQuery("JpaEntry.getAll", JpaEntry.class);

    if (page != null && pageSize != null) {
        query.setFirstResult(page * pageSize);
        query.setMaxResults(pageSize);
    }

    return query.getResultList();
}

From source file:com.epam.ipodromproject.repository.jpa.JPAUserRepository.java

@Override
public List<User> getUsersByPartOfName(String partOfName, int startingResult, int resultsCount, boolean enabled,
        boolean unblocked) {
    TypedQuery<User> query = entityManager.createNamedQuery("User.getUsersPageByPartOfNameAndEnabled",
            User.class);
    query.setParameter("code", "%" + partOfName + "%");
    query.setParameter("enabled", enabled);
    query.setParameter("blocked", !unblocked);
    return query.setMaxResults(resultsCount).setFirstResult(startingResult).getResultList();
}

From source file:com.carser.viamais.vo.TransactionFilter.java

public List<Transaction> resultList(final EntityManager em) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Transaction> cq = cb.createQuery(Transaction.class);
    Root<Transaction> transaction = cq.from(Transaction.class);
    cq.where(getPredicates(cb, transaction));
    @SuppressWarnings("rawtypes")
    Path path;/*from  w w  w  . j av  a2 s  . com*/
    switch (orderBy) {
    case CUSTOMER_NAME:
        path = transaction.get(Transaction_.car).get(Car_.model).get(Model_.name);
        break;
    case DATE:
        path = transaction.get(Transaction_.transactionDate);
        break;
    case PLATE:
        path = transaction.get(Transaction_.car).get(Car_.licensePlate);
        break;
    case TYPE:
        path = transaction.get(Transaction_.type);
        break;
    case YEAR:
        path = transaction.get(Transaction_.car).get(Car_.yearOfModel);
        break;
    default:
        path = transaction.get(Transaction_.car).get(Car_.model).get(Model_.name);
        break;
    }
    switch (order) {
    case DESC:
        cq.orderBy(cb.desc(path));
        break;
    default:
        cq.orderBy(cb.asc(path));
        break;
    }
    TypedQuery<Transaction> query = em.createQuery(cq);
    query.setFirstResult(offset);
    query.setMaxResults(limit);
    return query.getResultList();
}