Example usage for javax.persistence Query setFirstResult

List of usage examples for javax.persistence Query setFirstResult

Introduction

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

Prototype

Query setFirstResult(int startPosition);

Source Link

Document

Set the position of the first result to retrieve.

Usage

From source file:org.sofun.core.member.MemberServiceImpl.java

@SuppressWarnings("unchecked")
@Override//from   w  w w.j a v a  2s. co m
public List<MemberTransaction> getUnAckBonusTransactionFor(Member member) {

    String queryStr = "from " + MemberTransactionImpl.class.getSimpleName()
            + " t where t.member.id=:member_id AND t.bonus=:bonus AND t.ack=:ack ORDER BY txn_date DESC";
    Query query = createQuery(queryStr);
    query.setParameter("member_id", member.getId());
    query.setParameter("bonus", true);
    query.setParameter("ack", false);

    // TMP
    query.setFirstResult(0);
    query.setMaxResults(1);

    try {
        return query.getResultList();
    } catch (NoResultException nre) {
        return new ArrayList<MemberTransaction>();
    }

}

From source file:org.opentides.dao.impl.BaseEntityDaoJpaImpl.java

/**
 * {@inheritDoc}/*from  ww w.j  av  a  2  s  .  co m*/
 */
@Override
@SuppressWarnings("unchecked")
public final List<T> findByNamedQuery(final String name, int start, int total, final Object... params) {
    String queryString = getJpqlQuery(name);
    Query queryObject = getEntityManager().createQuery(queryString);
    if (params != null) {
        int i = 1;
        for (Object obj : params) {
            queryObject.setParameter(i, obj);
            i++;
        }
    }
    if (start > -1)
        queryObject.setFirstResult(start);
    if (total > -1)
        queryObject.setMaxResults(total);
    return queryObject.getResultList();
}

From source file:org.opencastproject.usertracking.impl.UserTrackingServiceImpl.java

@SuppressWarnings("unchecked")
public UserActionList getUserActionsByDay(String day, int offset, int limit) {
    UserActionList result = new UserActionListImpl();

    int year = Integer.parseInt(day.substring(0, 4));
    int month = Integer.parseInt(day.substring(4, 6)) - 1;
    int date = Integer.parseInt(day.substring(6, 8));

    Calendar calBegin = new GregorianCalendar();
    calBegin.set(year, month, date, 0, 0);
    Calendar calEnd = new GregorianCalendar();
    calEnd.set(year, month, date, 23, 59);

    result.setTotal(getTotal(calBegin, calEnd));
    result.setOffset(offset);/*from w w  w .  ja va2 s. com*/
    result.setLimit(limit);

    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("findUserActionsByIntervall");
        q.setParameter("begin", calBegin, TemporalType.TIMESTAMP);
        q.setParameter("end", calEnd, TemporalType.TIMESTAMP);
        q.setFirstResult(offset);
        q.setMaxResults(limit);
        Collection<UserAction> userActions = q.getResultList();

        for (UserAction a : userActions) {
            result.add(a);
        }
        return result;
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public List<FIREAdapter> readFIREAdaptersMetadata(Long categoryid, int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Query q;

    if ((categoryid != null) && (categoryid >= 0))
        q = entityManager.createQuery(//w  w w  .jav a 2  s .c  om
                "SELECT a FROM FIREAdapter a WHERE a.categories.id=" + categoryid + " ORDER BY a.id");
    else
        q = entityManager.createQuery("SELECT a FROM FIREAdapter a ORDER BY a.id");

    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);
    return q.getResultList();
}

From source file:org.medici.bia.dao.JpaDao.java

/**
 * Returns the first result of the provided query in a safe mode (it does not
 * throw exceptions if no result is found).
 *  //w  w  w.j  a v  a2 s  . co m
 * @param query the query
 * @return the first result found
 */
@SuppressWarnings("unchecked")
protected E getFirst(Query query) {
    query.setFirstResult(0);
    query.setMaxResults(1);

    List<E> results = (List<E>) query.getResultList();

    return results.size() == 1 ? results.get(0) : null;
}

From source file:org.asqatasun.entity.dao.audit.ContentDAOImpl.java

@Override
public List<Content> findOrphanRelatedContentList(WebResource webResource, int start, int chunkSize) {
    if (webResource instanceof Page) {
        Query query = entityManager.createQuery("SELECT distinct rc FROM " + RelatedContentImpl.class.getName()
                + RELATED_CONTENT_KEY + JOIN_PARENT_CONTENT_SET + JOIN_WR + WEB_RESOURCE_CONDITION
                + " AND rc.httpStatusCode =:httpStatusCode");
        query.setParameter(WEB_RESOURCE_KEY, webResource);
        query.setParameter(HTTP_STATUS_CODE_KEY, DEFAULT_HTTP_STATUS_VALUE);
        query.setFirstResult(start);
        query.setMaxResults(chunkSize);//from w  w  w.  jav  a 2 s  .  co  m
        return (List<Content>) query.getResultList();
    } else if (webResource instanceof Site) {
        Query query = entityManager.createQuery("SELECT distinct rc FROM " + RelatedContentImpl.class.getName()
                + RELATED_CONTENT_KEY + JOIN_PARENT_CONTENT_SET + JOIN_WR + " WHERE w.parent.id=:idWebResource"
                + " AND rc.httpStatusCode =:httpStatusCode");
        query.setParameter(ID_WEB_RESOURCE_KEY, webResource.getId());
        query.setParameter(HTTP_STATUS_CODE_KEY, DEFAULT_HTTP_STATUS_VALUE);
        query.setFirstResult(start);
        query.setMaxResults(chunkSize);
        List<Content> contentList = (List<Content>) query.getResultList();
        flushAndCloseEntityManager();
        return contentList;
    }
    return Collections.emptyList();
}

From source file:org.tanaguru.entity.dao.audit.ContentDAOImpl.java

@Override
public List<Content> findOrphanRelatedContentList(WebResource webResource, int start, int chunkSize) {
    if (webResource instanceof Page) {
        Query query = entityManager.createQuery("SELECT distinct rc FROM " + RelatedContentImpl.class.getName()
                + RELATED_CONTENT_KEY + JOIN_PARENT_CONTENT_SET + JOIN_WR + WEB_RESOURCE_CONDITION
                + " AND rc.httpStatusCode =:httpStatusCode");
        query.setParameter(WEB_RESOURCE_KEY, webResource);
        query.setParameter(HTTP_STATUS_CODE_KEY, DEFAULT_HTTP_STATUS_VALUE);
        query.setFirstResult(start);
        query.setMaxResults(chunkSize);//from  www. j a v a  2 s  .  co  m
        return (List<Content>) query.getResultList();
    } else if (webResource instanceof Site) {
        Query query = entityManager.createQuery("SELECT distinct rc FROM " + RelatedContentImpl.class.getName()
                + RELATED_CONTENT_KEY + JOIN_PARENT_CONTENT_SET + JOIN_WR + " WHERE w.parent.id=:idWebResource"
                + " AND rc.httpStatusCode =:httpStatusCode");
        query.setParameter(ID_WEB_RESOURCE_KEY, webResource.getId());
        query.setParameter(HTTP_STATUS_CODE_KEY, DEFAULT_HTTP_STATUS_VALUE);
        query.setFirstResult(start);
        query.setMaxResults(chunkSize);
        List<Content> contentList = (List<Content>) query.getResultList();
        flushAndCloseEntityManager();
        return contentList;
    }
    return Collections.EMPTY_LIST;
}

From source file:org.opens.tanaguru.entity.dao.audit.ContentDAOImpl.java

@Override
public List<Content> findOrphanRelatedContentList(WebResource webResource, int start, int chunkSize) {
    if (webResource instanceof Page) {
        Query query = entityManager.createQuery("SELECT distinct rc FROM " + RelatedContentImpl.class.getName()
                + RELATED_CONTENT_KEY + JOIN_PARENT_CONTENT_SET + JOIN_WR + WEB_RESOURCE_CONDITION
                + " AND rc.httpStatusCode =:httpStatusCode");
        query.setParameter(WEB_RESOURCE_KEY, webResource);
        query.setParameter(HTTP_STATUS_CODE_KEY, DEFAULT_HTTP_STATUS_VALUE);
        query.setFirstResult(start);
        query.setMaxResults(chunkSize);/*from  ww w .j  av  a 2s.  co  m*/
        return (List<Content>) query.getResultList();
    } else if (webResource instanceof Site) {
        Query query = entityManager.createQuery("SELECT distinct rc FROM " + RelatedContentImpl.class.getName()
                + RELATED_CONTENT_KEY + JOIN_PARENT_CONTENT_SET + JOIN_WR + " WHERE w.parent.id=:idWebResource"
                + " AND rc.httpStatusCode =:httpStatusCode");
        query.setParameter(ID_WEB_RESOURCE_KEY, webResource.getId());
        query.setParameter(HTTP_STATUS_CODE_KEY, DEFAULT_HTTP_STATUS_VALUE);
        query.setFirstResult(start);
        query.setMaxResults(chunkSize);
        List<Content> contentList = (List<Content>) query.getResultList();
        flushAndCloseEntityManager();
        return contentList;
    }
    return new ArrayList<Content>();
}

From source file:org.apache.syncope.core.persistence.jpa.dao.JPAAnySearchDAO.java

@Override
@SuppressWarnings("unchecked")
protected <T extends Any<?>> List<T> doSearch(final Set<String> adminRealms, final SearchCond cond,
        final int page, final int itemsPerPage, final List<OrderByClause> orderBy, final AnyTypeKind kind) {

    try {/*from   w  w  w  .j  ava2s  .c  o  m*/
        List<Object> parameters = Collections.synchronizedList(new ArrayList<>());

        SearchSupport svs = new SearchSupport(kind);

        Pair<String, Set<String>> filter = getAdminRealmsFilter(adminRealms, svs, parameters);

        // 1. get the query string from the search condition
        StringBuilder queryString = getQuery(buildEffectiveCond(cond, filter.getRight()), parameters, svs);

        // 2. take into account realms and ordering
        OrderBySupport obs = parseOrderBy(kind, svs, orderBy);
        if (queryString.charAt(0) == '(') {
            queryString.insert(0, buildSelect(obs));
            queryString.append(buildWhere(svs, obs));
        } else {
            queryString.insert(0, buildSelect(obs).append('('));
            queryString.append(')').append(buildWhere(svs, obs));
        }
        queryString.append(filter.getLeft()).append(buildOrderBy(obs));

        // 3. prepare the search query
        Query query = entityManager().createNativeQuery(queryString.toString());

        // 4. page starts from 1, while setFirtResult() starts from 0
        query.setFirstResult(itemsPerPage * (page <= 0 ? 0 : page - 1));

        if (itemsPerPage >= 0) {
            query.setMaxResults(itemsPerPage);
        }

        // 5. populate the search query with parameter values
        fillWithParameters(query, parameters);

        // 6. Prepare the result (avoiding duplicates)
        return buildResult(query.getResultList(), kind);
    } catch (Exception e) {
        LOG.error("While searching for {}", kind, e);
    }

    return Collections.emptyList();
}

From source file:org.opencastproject.usertracking.impl.UserTrackingServiceImpl.java

@SuppressWarnings("unchecked")
public UserActionList getUserActionsByTypeAndDay(String type, String day, int offset, int limit) {
    UserActionList result = new UserActionListImpl();

    int year = Integer.parseInt(day.substring(0, 4));
    int month = Integer.parseInt(day.substring(4, 6)) - 1;
    int date = Integer.parseInt(day.substring(6, 8));

    Calendar calBegin = new GregorianCalendar();
    calBegin.set(year, month, date, 0, 0);
    Calendar calEnd = new GregorianCalendar();
    calEnd.set(year, month, date, 23, 59);

    result.setTotal(getTotal(type, calBegin, calEnd));
    result.setOffset(offset);//from  ww  w  .j av  a  2  s  .c  o m
    result.setLimit(limit);

    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("findUserActionsByTypeAndIntervall");
        q.setParameter("type", type);
        q.setParameter("begin", calBegin, TemporalType.TIMESTAMP);
        q.setParameter("end", calEnd, TemporalType.TIMESTAMP);
        q.setFirstResult(offset);
        q.setMaxResults(limit);
        Collection<UserAction> userActions = q.getResultList();

        for (UserAction a : userActions) {
            result.add(a);
        }
        return result;
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}