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:ca.travelagency.persistence.query.QuerySQL.java

public static QuerySQL groupBy(Criteria criteria, EntityManager entityManager) {
    return new QuerySQL(criteria, entityManager) {
        @Override/* www .  j  a va2  s  . co  m*/
        protected String sqlAsString() {
            String groupBy = criteria.getGroupBy();
            Validate.notBlank(groupBy, "GroupBy query requires group by to be specified");
            return SELECT + DISTINCT + simpleName + Condition.SEPARATOR + groupBy + from() + where() + groupBy()
                    + orderBy();
        }

        @Override
        protected void addLimits(Query query) {
            if (criteria.getCount() != 0) {
                // FIXME JPA Query uses int we use long
                query.setMaxResults((int) criteria.getCount());
            }
        }
    };
}

From source file:org.ejbca.core.ejb.log.LogEntryData.java

/** @return a List<LogEntryData> from a custom SQL query. */
@SuppressWarnings("unchecked")
public static List<LogEntryData> findByCustomQueryAndPrivileges(EntityManager entityManager, String queryString,
        String caPriviledges, String viewLogPrivileges, int maxResults) {
    // Hibernate on DB2 wont allow us to "SELECT *" in combination with setMaxResults  
    String sql = "SELECT id, adminType, adminData, cAId, module, time, username, certificateSNR, event, logComment, rowVersion, rowProtection FROM LogEntryData WHERE ( "
            + queryString + ") AND (" + caPriviledges + ")";
    if (StringUtils.isNotEmpty(viewLogPrivileges)) {
        sql += " AND (" + viewLogPrivileges + ")";
    }//  w  w w . j  a  v  a  2 s  .c  om
    sql += " ORDER BY time DESC";
    if (log.isDebugEnabled()) {
        log.debug("Query: " + sql);
    }
    Query query = entityManager.createNativeQuery(sql, LogEntryData.class);
    query.setMaxResults(maxResults);
    return query.getResultList();
}

From source file:ca.travelagency.persistence.query.QuerySQL.java

public static QuerySQL find(Criteria criteria, EntityManager entityManager) {
    return new QuerySQL(criteria, entityManager) {
        @Override/*from   w  ww .  j  ava  2 s. com*/
        protected String sqlAsString() {
            return SELECT + DISTINCT + simpleName + from() + where() + orderBy();
        }

        @Override
        protected void addLimits(Query query) {
            if (criteria.getCount() != 0) {
                // FIXME JPA Query uses int we use long
                query.setFirstResult((int) criteria.getOffset());
                query.setMaxResults((int) criteria.getCount());
            }
        }
    };
}

From source file:org.querybyexample.jpa.JpaUtil.java

public static void applyPagination(Query query, SearchParameters sp) {
    if (sp.getFirst() > 0) {
        query.setFirstResult(sp.getFirst());
    }//from  ww w.  j  ava 2  s . c  o m
    if (sp.getPageSize() > 0) {
        query.setMaxResults(sp.getPageSize());
    } else if (sp.getMaxResults() > 0) {
        query.setMaxResults(sp.getMaxResults());
    }
}

From source file:com.siberhus.ngai.core.CrudHelper.java

@SuppressWarnings("unchecked")
public final static List<Object> findByQTO(EntityManager em, QTO qto) {
    Query query = createQueryObject(em, qto.buildQueryString(), qto.getParameterList());
    if (qto.getFirstResult() != null) {
        query.setFirstResult(qto.getFirstResult());
    }//  w ww. jav  a2s . c o  m
    if (qto.getMaxResult() != null) {
        query.setMaxResults(qto.getMaxResult());
    }
    return query.getResultList();
}

From source file:org.apache.oozie.tools.OozieDBExportCLI.java

private static int exportTableToJSON(Query query, ZipOutputStream zipOutputStream, String filename)
        throws IOException {
    Gson gson = new Gson();
    ZipEntry zipEntry = new ZipEntry(filename);
    zipOutputStream.putNextEntry(zipEntry);
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(zipOutputStream, "UTF-8"));
    query.setMaxResults(LIMIT);
    int exported = 0;
    List<?> list = query.getResultList();
    while (!list.isEmpty()) {
        query.setFirstResult(exported);/*from  w w  w.jav a2 s.co m*/
        list = query.getResultList();
        for (Object w : list) {
            exported++;
            gson.toJson(w, writer);
            writer.newLine();
        }
    }
    writer.flush();
    zipOutputStream.closeEntry();
    return exported;
}

From source file:org.apache.juddi.replication.ReplicationNotifier.java

/**
 * returns the latest version of the replication config or null if there
 * is no config/*from   w  w w. j a  v  a2 s. com*/
 *
 * @return
 */
public static org.uddi.repl_v3.ReplicationConfiguration FetchEdges() {

    EntityManager em = PersistenceManager.getEntityManager();
    EntityTransaction tx = null;
    org.uddi.repl_v3.ReplicationConfiguration item = new org.uddi.repl_v3.ReplicationConfiguration();
    try {
        tx = em.getTransaction();
        tx.begin();
        Query q = em
                .createQuery("SELECT item FROM ReplicationConfiguration item order by item.serialNumber DESC");
        q.setMaxResults(1);
        ReplicationConfiguration results = (ReplicationConfiguration) q.getSingleResult();
        //   ReplicationConfiguration find = em.find(ReplicationConfiguration.class, null);
        if (results != null) {
            MappingModelToApi.mapReplicationConfiguration(results, item);
        }

        tx.commit();
        return item;
    } catch (Exception ex) {
        //log.error("error", ex);
        //no config available
        if (tx != null && tx.isActive()) {
            tx.rollback();
        }
    } finally {
        em.close();
    }
    return null;
}

From source file:org.rhq.core.domain.server.PersistenceUtility.java

public static void setDataPage(Query query, PageControl pageControl) {
    if (pageControl.getPageSize() > 0) {
        query.setFirstResult(pageControl.getStartRow());
        query.setMaxResults(pageControl.getPageSize());
    }/*from  w  w w .ja  va  2 s .  c o m*/
}

From source file:org.rhq.core.domain.server.PersistenceUtility.java

/**
 * Creates and executes a filter query for a collection relationship. This executes without passing back the query
 * object because the most common case is to simply paginate for a relationship. Use the createFilter method to
 * create more generic filters and get access to the hibernate query object for setting parameters etc.
 *
 * @param  entityManager/*from  w  w w.ja  va 2s  . c  om*/
 * @param  collection
 * @param  pageControl
 *
 * @return the result list of the entities from the filtered relationship
 */
@SuppressWarnings("unchecked")
public static PageList createPaginationFilter(EntityManager entityManager, Collection collection,
        PageControl pageControl) {
    if (collection == null) {
        return new PageList(pageControl);
    }

    String filter = "";
    if (pageControl.getPrimarySortColumn() != null) {
        PageOrdering order = (pageControl.getPrimarySortOrder() == null) ? PageOrdering.ASC
                : pageControl.getPrimarySortOrder();
        filter = getOrderByFragment(new OrderingField(pageControl.getPrimarySortColumn(), order));
    }

    org.hibernate.Query query = getHibernateSession(entityManager).createFilter(collection, filter);
    if (pageControl.getPageSize() > 0) {
        query.setFirstResult(pageControl.getPageNumber() * pageControl.getPageSize());
        query.setMaxResults(pageControl.getPageSize());
    }

    // TODO GH: Always flushing is probably not what we really want here
    // relationship filters don't seem to cause the proper flush, so manually flush
    getHibernateSession(entityManager).flush();

    // TODO GH: This can only create unbounded PageLists since I don't know how to do a count query to find the size
    return new PageList<Object>(query.list(), pageControl);
}

From source file:org.ejbca.core.ejb.ra.UserData.java

/** @return return a List<UserData> matching the custom query. */
@SuppressWarnings("unchecked")
public static List<UserData> findByCustomQuery(EntityManager entityManager, String customQuery,
        int maxResults) {
    final Query query = entityManager.createQuery("SELECT a FROM UserData a WHERE " + customQuery);
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }//from  w ww  .j  a  va2 s . c o m
    return query.getResultList();
}