Example usage for javax.persistence Query setMaxResults

List of usage examples for javax.persistence Query setMaxResults

Introduction

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

Prototype

Query setMaxResults(int maxResult);

Source Link

Document

Set the maximum number of results to retrieve.

Usage

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

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

    if ((categoryid != null) && (categoryid >= 0))
        q = entityManager/*w w w .  ja  v  a 2 s  . c o  m*/
                .createQuery("SELECT a FROM Widget a WHERE a.categories.id=" + categoryid + " ORDER BY a.id");
    else
        q = entityManager.createQuery("SELECT a FROM Widget a ORDER BY a.id");
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);
    return q.getResultList();
}

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

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

    if ((categoryid != null) && (categoryid >= 0))
        q = entityManager//from  w  w w.j ava2  s.  c  om
                .createQuery("SELECT a FROM Course a WHERE a.categories.id=" + categoryid + " ORDER BY a.id");
    else
        q = entityManager.createQuery("SELECT a FROM Course a ORDER BY a.id");
    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);
    return q.getResultList();
}

From source file:org.apache.roller.weblogger.business.jpa.JPAWeblogManagerImpl.java

public List getWeblogsByLetter(char letter, int offset, int length) throws WebloggerException {
    Query query = strategy.getNamedQuery("Weblog.getByLetterOrderByHandle");
    query.setParameter(1, letter + "%");
    if (offset != 0) {
        query.setFirstResult(offset);//  ww w . jav  a 2s. c  o m
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return query.getResultList();
}

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

@SuppressWarnings("unchecked")
public List<Product> readProducts(Long categoryid, int firstResult, int maxResults) {
    EntityManager entityManager = entityManagerFactory.createEntityManager();
    Query q;

    if ((categoryid != null) && (categoryid >= 0))
        q = entityManager//from  w ww. j av a2s  . c  om
                .createQuery("SELECT a FROM Product a WHERE a.category.id=" + categoryid + " ORDER BY a.id");
    else
        q = entityManager.createQuery("SELECT a FROM Product a ORDER BY a.id");

    q.setFirstResult(firstResult);
    q.setMaxResults(maxResults);
    return q.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 . j a va  2  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:org.apache.roller.weblogger.business.jpa.JPAUserManagerImpl.java

public List getUsersByLetter(char letter, int offset, int length) throws WebloggerException {
    Query query = strategy.getNamedQuery("User.getByUserNameOrderByUserName");
    query.setParameter(1, letter + "%");
    if (offset != 0) {
        query.setFirstResult(offset);/*ww  w .j a  va  2s.co  m*/
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    return query.getResultList();
}

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(/*from  w w  w  .j  a v  a2 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).
 *  /*from   w  w  w . j  a v  a2  s  .  c o  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.apache.roller.weblogger.business.jpa.JPAWeblogManagerImpl.java

/**
 * Get websites of a user/*from www  .j av a2 s .  com*/
 */
public List getWeblogs(Boolean enabled, Boolean active, Date startDate, Date endDate, int offset, int length)
        throws WebloggerException {

    //if (endDate == null) endDate = new Date();

    List params = new ArrayList();
    int size = 0;
    StringBuffer queryString = new StringBuffer();
    StringBuffer whereClause = new StringBuffer();

    queryString.append("SELECT w FROM Weblog w WHERE ");

    if (startDate != null) {
        Timestamp start = new Timestamp(startDate.getTime());
        if (whereClause.length() > 0)
            whereClause.append(" AND ");
        params.add(size++, start);
        whereClause.append(" w.dateCreated > ?" + size);
    }
    if (endDate != null) {
        Timestamp end = new Timestamp(endDate.getTime());
        if (whereClause.length() > 0)
            whereClause.append(" AND ");
        params.add(size++, end);
        whereClause.append(" w.dateCreated < ?" + size);
    }
    if (enabled != null) {
        if (whereClause.length() > 0)
            whereClause.append(" AND ");
        params.add(size++, enabled);
        whereClause.append(" w.enabled = ?" + size);
    }
    if (active != null) {
        if (whereClause.length() > 0)
            whereClause.append(" AND ");
        params.add(size++, active);
        whereClause.append(" w.active = ?" + size);
    }

    whereClause.append(" ORDER BY w.dateCreated DESC");

    Query query = strategy.getDynamicQuery(queryString.toString() + whereClause.toString());
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    for (int i = 0; i < params.size(); i++) {
        query.setParameter(i + 1, params.get(i));
    }

    return query.getResultList();
}

From source file:net.firejack.platform.core.store.BaseStore.java

@Transactional(readOnly = true)
protected List<E> findByQuery(final Integer offset, final Integer limit, final String queryStr,
        final Map queryParams, final ResultTransformer transformer) {
    return getHibernateTemplate().execute(new HibernateCallback<List<E>>() {
        public List<E> doInHibernate(Session session) throws HibernateException, SQLException {
            Query query = session.createQuery(queryStr);
            if (offset != null && offset > -1) {
                query.setFirstResult(offset);
            }/* w w w .j  a  va2 s . c om*/
            if (limit != null && limit > -1) {
                query.setMaxResults(limit);
            }
            query.setProperties(queryParams);
            query.setResultTransformer(transformer);
            return query.list();
        }
    });
}