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:net.dontdrinkandroot.persistence.dao.GenericJpaDao.java

/**
 * Finds all entities by the given {@link CriteriaQuery} and limit the result set.
 * //from  ww w  .  ja  v a2s  .co  m
 * @param firstResult
 *            Position of the first result to retrive.
 * @param maxResults
 *            The maximum number of results to retrieve.
 */
protected <V> List<V> find(final CriteriaQuery<V> criteriaQuery, final int firstResult, final int maxResults) {
    final TypedQuery<V> query = this.getEntityManager().createQuery(criteriaQuery);
    query.setMaxResults(maxResults);
    query.setFirstResult(firstResult);

    return query.getResultList();
}

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

/**
 * Gets the document at the given rank./*from   w w w  .  j ava 2s .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)/*from  w w w.j  ava2s .  c  om*/
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.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";
    }/* ww w. j a v  a 2s. 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:de.taimos.dao.hibernate.EntityDAOHibernate.java

private <T> List<T> findListByQueryLimit(final String query, Class<T> clazz, final int first, final int max,
        final Object... params) {
    final TypedQuery<T> tq = this.entityManager.createQuery(query, clazz);
    for (int i = 0; i < params.length; i++) {
        tq.setParameter(i + 1, params[i]);
    }/*from   w w w. j  av a2s  .  co m*/
    if (first >= 0) {
        tq.setFirstResult(first);
    }
    if (max >= 0) {
        tq.setMaxResults(max);
    }
    return tq.getResultList();
}

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

private List<Account> getSearchResultList(String searchTerm, int count, int offset) {
    String qlString = "SELECT a FROM Account a WHERE a.accountPath like :q "
            + "OR a.owner.name like :q OR a.owner.email like :q";

    List<Account> accounts = null;
    try {//from   w  w w .  java 2  s.co  m
        TypedQuery<Account> query = em.createQuery(qlString, Account.class);
        query.setParameter("q", searchTerm);
        query.setMaxResults(count);
        query.setFirstResult(offset);

        accounts = query.getResultList();
    } catch (NoResultException e) {
        // Do nothing;
    }
    return accounts;

}

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;/*ww  w  .java 2s .  co  m*/
    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();
}

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

public List<Piece> getTopPieces(int page, int pageSize) {
    TypedQuery<Piece> query = getEntityManager().createQuery(
            "SELECT piece FROM Piece piece WHERE piece.likes > 0 ORDER BY likes DESC, generationTime DESC",
            Piece.class);
    query.setFirstResult(page * pageSize);
    query.setMaxResults(pageSize);/*from w w w. j a v  a 2 s  .  c  o m*/

    return query.getResultList();
}

From source file:org.verinice.persistence.Dao.java

/**
 * Configures the query and sets the maximum number and the first element 
 * which are returned from the result.//from w ww.  j  a v a2s.  co  m
 * 
 * @param query The JPA query
 * @param size The maximum number of elements which are returned from the
 *    result of the query.
 * @param firstResult The index of the first element which is returned from 
 *    the result of the query
 */
protected void configureResultSize(TypedQuery<?> query, Integer size, Integer firstResult) {
    int firstIndex;
    if (firstResult == null || firstResult < 0) {
        firstIndex = 0;
    } else {
        firstIndex = firstResult;
    }
    query.setFirstResult(firstIndex);
    int limit = getLimit(size);
    logger.debug("Result limit: " + limit);
    query.setMaxResults(limit);
}

From source file:cn.newgxu.lab.core.repository.impl.AbstractCommonDaoImpl.java

/**
 * ?<b style="color: red;"> :xxx </b>?????
 * @param hql hql// w  w  w .jav  a 2  s. c o m
 * @param clazz 
 * @param offset ??????
 * @param number ????
 * @param params ?map?
 * @return 
 */
protected List<T> executeQuery(String hql, Class<T> clazz, int offset, int number, Map<String, Object> params) {
    offset = rangeCheck(offset, clazz);
    TypedQuery<T> query = em.createNamedQuery(hql, clazz);
    if (params != null) {
        for (String name : params.keySet()) {
            query.setParameter(name, params.get(name));
        }
    }
    return query.setFirstResult(offset).setMaxResults(number).getResultList();
}