Example usage for javax.persistence TypedQuery setFirstResult

List of usage examples for javax.persistence TypedQuery setFirstResult

Introduction

In this page you can find the example usage for javax.persistence TypedQuery setFirstResult.

Prototype

TypedQuery<X> setFirstResult(int startPosition);

Source Link

Document

Set the position of the first result to retrieve.

Usage

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

@Override
public List<Speaker> findAll(int page, int pageSize) {
    TypedQuery<Speaker> query = em.createQuery("select s from Speaker s", Speaker.class);

    query.setFirstResult(page * pageSize);
    query.setMaxResults(pageSize);//  w w w  .  ja  v a2  s  .  c  o m

    return query.getResultList();
}

From source file:com.nuevebit.persistence.repository.JPASearchableRepository.java

@Override
public final List<T> search(S searchCriteria, Pageable pageable) {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<T> query = cb.createQuery(getEntityClass());

    Root<T> p = createQueryRoot(query, searchCriteria);
    query.select(p);/*ww  w.jav  a2s . c o  m*/

    Order sortOrder = getSortOrder(cb);
    if (sortOrder != null) {
        query.orderBy(sortOrder);
    }

    TypedQuery<T> typedQuery = getEntityManager().createQuery(query);
    if (pageable != null) {
        typedQuery.setFirstResult(pageable.getOffset()).setMaxResults(pageable.getPageSize());
    }

    return typedQuery.getResultList();
}

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

public List<LicType_entType> getLicType_entTypeList(int page, int rows) {
    int head = (page - 1) * rows;
    String jpql = "select l from LicType_entType l where 1=1 ";
    TypedQuery<LicType_entType> query = em().createQuery(jpql, LicType_entType.class);
    //query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
    query.setFirstResult(head);
    query.setMaxResults(rows);/*w w w. j a v  a 2  s  . c  o m*/
    List<LicType_entType> list = query.getResultList();
    return list;
}

From source file:example.springdata.jpa.showcase.before.CustomerServiceImpl.java

@Override
public List<Customer> findAll(int page, int pageSize) {

    TypedQuery<Customer> query = em.createQuery("select c from Customer c", Customer.class);

    query.setFirstResult(page * pageSize);
    query.setMaxResults(pageSize);//from   w w  w .ja v  a 2  s .  c o  m

    return query.getResultList();
}

From source file:net.triptech.metahive.model.Record.java

/**
 * Find record entries./*from   w w  w  .  ja  va 2  s  . c o  m*/
 *
 * @param filter the record filter
 * @param firstResult the first result
 * @param maxResults the max results
 * @return the list
 */
public static List<Record> findRecordEntries(final RecordFilter filter, final List<Definition> definitions,
        final UserRole userRole, final ApplicationContext context, final int firstResult,
        final int maxResults) {

    List<Record> records = new ArrayList<Record>();

    Map<String, Object> variables = new HashMap<String, Object>();

    Definition orderDefinition = null;
    if (filter.getOrderId() != null && filter.getOrderId() > 0) {
        orderDefinition = Definition.findDefinition(filter.getOrderId());
    }

    StringBuilder sql = new StringBuilder("SELECT DISTINCT r FROM Record r");

    if (orderDefinition != null && orderDefinition.getId() != null) {
        sql.append(" LEFT OUTER JOIN r.keyValues as o");
        sql.append(" WITH o.definition.id = :orderDefinitionId");

        variables.put("orderDefinitionId", orderDefinition.getId());
    }

    Map<String, Map<String, Object>> whereParameters = buildWhere(filter);

    if (whereParameters.size() > 0) {
        String sqlWhere = whereParameters.keySet().iterator().next();
        Map<String, Object> whereVariables = whereParameters.get(sqlWhere);

        for (String key : whereVariables.keySet()) {
            variables.put(key, whereVariables.get(key));
        }
        sql.append(sqlWhere);
    }

    String orderValueCol = "r.recordId";
    if (orderDefinition != null && orderDefinition.getId() != null) {
        orderValueCol = "o.doubleValue";
        if (orderDefinition.getDataType() == DataType.TYPE_STRING) {
            orderValueCol = "o.stringValue";
        }
        if (orderDefinition.getDataType() == DataType.TYPE_BOOLEAN) {
            orderValueCol = "o.booleanValue";
        }
    }

    sql.append(" ORDER BY " + orderValueCol);

    if (filter.isOrderDescending()) {
        sql.append(" DESC");
    } else {
        sql.append(" ASC");
    }

    if (orderDefinition != null && orderDefinition.getId() != null) {
        sql.append(", r.recordId ASC");
    }

    logger.info("SQL: " + sql.toString());

    TypedQuery<Record> q = entityManager().createQuery(sql.toString(), Record.class);

    if (maxResults > 0) {
        q.setFirstResult(firstResult).setMaxResults(maxResults);
    }

    for (String variable : variables.keySet()) {
        q.setParameter(variable, variables.get(variable));
    }

    for (Record record : q.getResultList()) {
        record.setKeyValues(KeyValue.findKeyValues(record, definitions), definitions, userRole, context);
        records.add(record);
    }
    return records;
}

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: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 {/* ww w  .j av  a2 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.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 ww . j a v  a 2  s  .c om
    query.setFirstResult(page * pageSize);
    query.setMaxResults(pageSize);

    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));
    }/*from   w w  w .j  av a2  s  . c  om*/
    /**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: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);//from  ww w. j  a v 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();
}