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.mitre.oauth2.repository.impl.JpaAuthenticationHolderRepository.java

@Override
@Transactional(value = "defaultTransactionManager")
public List<AuthenticationHolderEntity> getOrphanedAuthenticationHolders() {
    TypedQuery<AuthenticationHolderEntity> query = manager
            .createNamedQuery(AuthenticationHolderEntity.QUERY_GET_UNUSED, AuthenticationHolderEntity.class);
    query.setMaxResults(MAXEXPIREDRESULTS);
    List<AuthenticationHolderEntity> unusedAuthenticationHolders = query.getResultList();
    return unusedAuthenticationHolders;
}

From source file:org.mmonti.entitygraph.repository.CustomGenericJpaRepository.java

@Override
protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, Specification<T> spec) {
    query.setFirstResult(pageable.getOffset());
    query.setMaxResults(pageable.getPageSize());

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

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

From source file:csns.model.site.dao.jpa.SiteDaoImpl.java

@Override
public List<Site> getSites(Course course, User instructor, int maxResults) {
    String query = "select s from Site s join s.section.instructors i "
            + "where s.section.course = :course and (s.shared = true or i = :instructor) "
            + "order by s.section.quarter desc, s.section.number asc";

    TypedQuery<Site> typedQuery = entityManager.createQuery(query, Site.class).setParameter("course", course)
            .setParameter("instructor", instructor);
    if (maxResults > 0)
        typedQuery.setMaxResults(maxResults);

    return typedQuery.getResultList();
}

From source file:eu.domibus.common.dao.ErrorLogDao.java

public List<ErrorLogEntry> findPaged(int from, int max, String column, boolean asc,
        HashMap<String, Object> filters) {
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<ErrorLogEntry> cq = cb.createQuery(ErrorLogEntry.class);
    Root<ErrorLogEntry> ele = cq.from(ErrorLogEntry.class);
    cq.select(ele);/*  w ww. j  av a2 s  .c o m*/
    List<Predicate> predicates = new ArrayList<Predicate>();
    for (Map.Entry<String, Object> filter : filters.entrySet()) {
        if (filter.getValue() != null) {
            if (filter.getValue() instanceof String) {
                if (!filter.getValue().toString().isEmpty()) {
                    switch (filter.getKey().toString()) {
                    case "":
                        break;
                    case "timestampFrom":
                        predicates.add(cb.greaterThanOrEqualTo(ele.<Date>get("timestamp"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    case "timestampTo":
                        predicates.add(cb.lessThanOrEqualTo(ele.<Date>get("timestamp"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    case "notifiedFrom":
                        predicates.add(cb.greaterThanOrEqualTo(ele.<Date>get("notified"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    case "notifiedTo":
                        predicates.add(cb.lessThanOrEqualTo(ele.<Date>get("notified"),
                                Timestamp.valueOf(filter.getValue().toString())));
                        break;
                    default:
                        predicates.add(cb.like(ele.<String>get(filter.getKey()), (String) filter.getValue()));
                        break;
                    }
                }
            } else {
                predicates.add(cb.equal(ele.<String>get(filter.getKey()), filter.getValue()));
            }
        }
    }
    cq.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
    if (column != null) {
        if (asc) {
            cq.orderBy(cb.asc(ele.get(column)));
        } else {
            cq.orderBy(cb.desc(ele.get(column)));
        }

    }
    final TypedQuery<ErrorLogEntry> query = this.em.createQuery(cq);
    query.setFirstResult(from);
    query.setMaxResults(max);
    return query.getResultList();
}

From source file:com.jcertif.abj2014.intro.spring.data.before.SpeakerServiceImpl.java

@Override
public List<Speaker> findByLastname(String lastname, int page, int pageSize) {
    TypedQuery<Speaker> query = em.createQuery("select s from Speaker s where s.lastname = ?1", Speaker.class);

    query.setParameter(1, lastname);/*from   w  w  w  .j  a  va2 s  .c  om*/
    query.setFirstResult(page * pageSize);
    query.setMaxResults(pageSize);

    return query.getResultList();
}

From source file:com.sixsq.slipstream.persistence.Run.java

public static List<RunView> viewList(User user, String moduleResourceUri, Integer offset, Integer limit,
        String cloudServiceName) throws ConfigurationException, ValidationException {
    List<RunView> views = null;
    EntityManager em = PersistenceUtil.createEntityManager();
    try {/*from   w  w w .j a  va  2  s. c om*/
        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<Run> critQuery = builder.createQuery(Run.class);
        Root<Run> rootQuery = critQuery.from(Run.class);
        critQuery.select(rootQuery);
        Predicate where = viewListCommonQueryOptions(builder, rootQuery, user, moduleResourceUri,
                cloudServiceName);
        if (where != null) {
            critQuery.where(where);
        }
        critQuery.orderBy(builder.desc(rootQuery.get("startTime")));
        TypedQuery<Run> query = em.createQuery(critQuery);
        if (offset != null) {
            query.setFirstResult(offset);
        }
        query.setMaxResults((limit != null) ? limit : DEFAULT_LIMIT);
        List<Run> runs = query.getResultList();
        views = convertRunsToRunViews(runs);
    } finally {
        em.close();
    }
    return views;
}

From source file:com.music.dao.PieceDao.java

public List<Piece> getByPreferences(UserPreferences preferences) {
    String queryStr = "SELECT piece FROM Piece piece ORDER BY likes DESC, generationTime DESC";
    if (!preferences.isDefault()) {
        queryStr = addPreferenceCriteria(preferences, "SELECT piece FROM Piece piece WHERE ")
                + " ORDER BY likes DESC, generationTime DESC";
    }/*from w  w w . ja  v  a2  s. c o m*/
    TypedQuery<Piece> query = getEntityManager().createQuery(queryStr, Piece.class);
    query.setMaxResults(30);

    return query.getResultList();
}

From source file:com.june.app.board.repository.jpa.BoardRepositoryImpl.java

@Override
public Collection<Board> boardListWithPaging(Board vo) {
    int bbsId = vo.getBbsId();
    int pageSize = vo.getPageSize();
    int pageNumber = (int) vo.getPageIndex();

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<Board> criteriaQuery = criteriaBuilder.createQuery(Board.class);

    Root<Board> from = criteriaQuery.from(Board.class);
    CriteriaQuery<Board> select = criteriaQuery.select(from);
    if (bbsId > 0) {
        criteriaQuery.where(criteriaBuilder.equal(from.get("bbsId"), bbsId));
    }/*  www  . j ava  2 s. c o  m*/
    /**list desc for date*/
    criteriaQuery.orderBy(criteriaBuilder.desc(from.get("frstRegistPnttm")));
    TypedQuery<Board> typedQuery = em.createQuery(select);

    typedQuery.setFirstResult((pageNumber - 1) * pageSize);
    typedQuery.setMaxResults(pageSize);

    Collection<Board> fooList = typedQuery.getResultList();

    return fooList;

}

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

public List<Object[]> findPurchaseList(Date beginDate, Date endDate, Integer count) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class);
    Root<Member> member = criteriaQuery.from(Member.class);
    Join<Product, Order> orders = member.join("orders");
    criteriaQuery.multiselect(member.get("id"), member.get("username"), member.get("email"),
            member.get("point"), member.get("amount"), member.get("balance"),
            criteriaBuilder.sum(orders.<BigDecimal>get("amountPaid")));
    Predicate restrictions = criteriaBuilder.conjunction();
    if (beginDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.greaterThanOrEqualTo(orders.<Date>get("createDate"), beginDate));
    }/*from   w  w  w.j av a  2 s.  c  o m*/
    if (endDate != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.lessThanOrEqualTo(orders.<Date>get("createDate"), endDate));
    }
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.equal(orders.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.equal(orders.get("paymentStatus"), PaymentStatus.paid));
    criteriaQuery.where(restrictions);
    criteriaQuery.groupBy(member.get("id"), member.get("username"), member.get("email"), member.get("point"),
            member.get("amount"), member.get("balance"));
    criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum(orders.<BigDecimal>get("amountPaid"))));
    TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT);
    if (count != null && count >= 0) {
        query.setMaxResults(count);
    }
    return query.getResultList();
}

From source file:com.music.dao.PieceDao.java

public Piece getNewlyCreatedPiece(UserPreferences preferences) {
    String queryString = "SELECT piece FROM Piece piece WHERE piece.newlyCreated = true ORDER BY generationTime DESC";
    if (!preferences.isDefault()) {
        queryString = addPreferenceCriteria(preferences,
                "SELECT piece FROM Piece piece WHERE piece.newlyCreated = true AND ")
                + " ORDER BY generationTime DESC";
    }//from   w  ww  . ja v a  2 s .co m
    TypedQuery<Piece> query = getEntityManager().createQuery(queryString, Piece.class);
    query.setMaxResults(1);

    try {
        return query.getSingleResult();
    } catch (NoResultException ex) {
        return null;
    }
}