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:org.openmeetings.app.data.basic.Configurationmanagement.java

public List<Configuration> getConfigurations(int start, int max, String orderby, boolean asc) {
    try {/*from w w  w. ja  v  a 2 s . c o  m*/
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Configuration> cq = cb.createQuery(Configuration.class);
        Root<Configuration> c = cq.from(Configuration.class);
        Predicate condition = cb.equal(c.get("deleted"), "false");
        cq.where(condition);
        cq.distinct(asc);
        if (asc) {
            cq.orderBy(cb.asc(c.get(orderby)));
        } else {
            cq.orderBy(cb.desc(c.get(orderby)));
        }
        TypedQuery<Configuration> q = em.createQuery(cq);
        q.setFirstResult(start);
        q.setMaxResults(max);
        List<Configuration> ll = q.getResultList();
        return ll;
    } catch (Exception ex2) {
        log.error("[getConfigurations]", ex2);
    }
    return null;
}

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

/**
 * ?<b style="color: red;"> ? </b>?????
 * @param hql hql// w ww .  java  2s .c  o m
 * @param clazz 
 * @param offset ??????
 * @param number ????
 * @param objects ?null?
 * @return 
 */
protected List<T> executeQuery(String hql, Class<T> clazz, int offset, int number, Object... objects) {
    offset = rangeCheck(offset, clazz);
    TypedQuery<T> query = em.createNamedQuery(hql, clazz);
    if (objects != null) {
        for (int i = 0; i < objects.length; i++) {
            query.setParameter(i + 1, objects[i]);
        }
    }
    return query.setFirstResult(offset).setMaxResults(number).getResultList();
}

From source file:org.businessmanager.dao.GenericDaoImpl.java

@Override
public List<T> findAll(SingularAttribute<T, ?> orderAttribute, boolean orderAsc, int firstResult,
        int maxResults, Map<SingularAttribute<T, ?>, Object> filterAttributes, boolean enableLikeSearch) {
    CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder();

    CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass());
    Root<T> rootQuery = criteriaQuery.from(getPersistenceClass());

    CriteriaQuery<T> select = criteriaQuery.select(rootQuery);

    List<Predicate> predicateList = createFilterList(filterAttributes, enableLikeSearch, queryBuilder,
            rootQuery);/*from w  ww  .  j av  a2 s . c om*/
    select.where(predicateList.toArray(new Predicate[0]));

    if (orderAsc) {
        criteriaQuery.orderBy(queryBuilder.asc(rootQuery.get(orderAttribute)));
    } else {
        criteriaQuery.orderBy(queryBuilder.desc(rootQuery.get(orderAttribute)));
    }

    TypedQuery<T> typedQuery = entityManager.createQuery(criteriaQuery);

    if (firstResult != -1) {
        typedQuery.setFirstResult(firstResult);
    }
    if (maxResults != -1) {
        typedQuery.setMaxResults(maxResults);
    }

    return typedQuery.getResultList();
}

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

public List<LicenseTypeReturn> getLicenseTypeList(int page) {
    try {/*from   w  w w . j  a v  a 2  s.  c  om*/
        String jpql = LicenseTypeSQL(page);
        TypedQuery<LicenseTypeReturn> query = em().createQuery(jpql, LicenseTypeReturn.class);
        query.setHint(QueryHints.RESULT_TYPE, ResultType.Map);
        int head = (page - 1) * 15;
        query.setFirstResult(head);
        query.setMaxResults(15);
        List<LicenseTypeReturn> LicenseTypeList = query.getResultList();
        System.out.println(" ??sql??");
        return LicenseTypeList;
    } catch (Exception e) {
        // TODO: handle exception
        System.out.println("??sql?" + e.getMessage());
        return null;
    }
}

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

public List<Piece> getUserPieces(Long userId, int page, int pageSize) {
    TypedQuery<Piece> query = getEntityManager().createQuery(
            "SELECT pe.piece FROM PieceEvaluation pe WHERE pe.user.id=:userId AND pe.positive = true ORDER BY pe.dateTime DESC",
            Piece.class);
    query.setParameter("userId", userId);
    query.setFirstResult(page * pageSize);
    query.setMaxResults(pageSize);//from   w  w  w . j  a  va2 s.c  om

    return query.getResultList();
}

From source file:eu.ggnet.dwoss.report.ReportAgentBean.java

@Override
public List<ReportLine> find(SearchParameter search, int firstResult, int maxResults) {
    StringBuilder sb = new StringBuilder("Select l from ReportLine l");
    if (!StringUtils.isBlank(search.getRefurbishId()))
        sb.append(" where l.refurbishId = :refurbishId");
    L.debug("Using created SearchQuery:{}", sb);
    TypedQuery<ReportLine> q = reportEm.createQuery(sb.toString(), ReportLine.class);
    if (!StringUtils.isBlank(search.getRefurbishId()))
        q.setParameter("refurbishId", search.getRefurbishId().trim());
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);/*from  w ww . ja  v  a 2  s  .  c o  m*/
    return q.getResultList();
}

From source file:org.synyx.hades.dao.orm.GenericJpaDao.java

/**
 * @param query//from  ww w  . ja  va 2  s.  com
 * @param spec
 * @param pageable
 * @return
 */
private Page<T> readPage(final TypedQuery<T> query, final Pageable pageable, final Specification<T> spec) {

    query.setFirstResult(pageable.getFirstItem());
    query.setMaxResults(pageable.getPageSize());

    return new PageImpl<T>(query.getResultList(), pageable, count(spec));
}

From source file:eu.ggnet.dwoss.report.ReportAgentBean.java

@Override
public List<SimpleReportLine> findSimple(SearchParameter search, int firstResult, int maxResults) {
    StringBuilder sb = new StringBuilder("Select l from SimpleReportLine l");
    if (!StringUtils.isBlank(search.getRefurbishId()))
        sb.append(" where l.refurbishId = :refurbishId");
    L.debug("Using created SearchQuery:{}", sb);
    TypedQuery<SimpleReportLine> q = reportEm.createQuery(sb.toString(), SimpleReportLine.class);
    if (!StringUtils.isBlank(search.getRefurbishId()))
        q.setParameter("refurbishId", search.getRefurbishId().trim());
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);/*  w  w  w .j  a  va 2 s  .  c om*/
    return q.getResultList();
}

From source file:eu.europa.ec.fisheries.uvms.exchange.dao.bean.ExchangeLogDaoBean.java

@Override
public List<ExchangeLog> getExchangeLogListPaginated(Integer page, Integer listSize, String sql,
        List<SearchValue> searchKeyValues) throws ExchangeDaoException {
    try {// w  ww  .j ava2  s  .  c om
        LOG.debug("SQL QUERY IN LIST PAGINATED: " + sql);
        TypedQuery<ExchangeLog> query = em.createQuery(sql, ExchangeLog.class);

        HashMap<ExchangeSearchField, List<SearchValue>> orderedValues = SearchFieldMapper
                .combineSearchFields(searchKeyValues);

        setQueryParameters(query, orderedValues);

        query.setFirstResult(listSize * (page - 1));
        query.setMaxResults(listSize);

        return query.getResultList();
    } catch (IllegalArgumentException e) {
        LOG.error("[ Error getting exchangelog list paginated ] {}", e.getMessage());
        throw new ExchangeDaoException("[ Error when getting list ] ");
    } catch (Exception e) {
        LOG.error("[ Error getting exchangelog list paginated ]  {}", e.getMessage());
        throw new ExchangeDaoException("[ Error when getting list ] ");
    }
}

From source file:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java

/**
 * @param length/*from w w w .j a v  a  2 s  . com*/
 *            maxResult
 * @param start
 *            pagination starting point
 * @param search
 *            search string like ' name: toto , dupont' to search for %toto% on name + %dupont% in all properties
 * @param searchProperties
 *            properties to search or all if null
 * @param orders
 *            sorted result by properties
 * @return
 */
public List<ENTITY> findFiltered(Integer length, Integer start, String search, List<String> searchProperties,
        List<Order> orders) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<ENTITY> query = builder.createQuery(entityClass);
    Root<ENTITY> root = query.from(entityClass);

    if (!Strings.isNullOrEmpty(search)) {
        Predicate[] buildFilterPredicates = BuildFilterPredicates(root, search, searchProperties);
        if (buildFilterPredicates.length > 0) {
            query.where(builder.or(buildFilterPredicates));
        }
    }

    if (orders != null) {
        query.orderBy(OrderToOrder.toJpa(builder, root, orders));
    }

    TypedQuery<ENTITY> q = entityManager.createQuery(query);
    if (start != null) {
        q.setFirstResult(start);
    }
    if (length != null) {
        q.setMaxResults(length);
    }
    return findList(q);
}