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:org.ejbca.core.ejb.audit.EjbcaAuditorSessionBean.java

@SuppressWarnings("unchecked")
@Override//from   w w  w .j  a  v a2 s  .com
public List<? extends AuditLogEntry> selectAuditLog(final AuthenticationToken token, final String device,
        final int firstResult, final int maxResults, final String whereClause, final String orderClause,
        final List<Object> parameters) throws AuthorizationDeniedException {
    if (!IntegrityProtectedDevice.class.getSimpleName().equals(device)) {
        throw new UnsupportedOperationException(
                "selectAuditLog can only be used with " + IntegrityProtectedDevice.class.getSimpleName());
    }
    // Require that the caller is authorized to AUDITLOGSELECT just like in org.cesecore.audit.audit.SecurityEventsAuditorSessionBean.selectAuditLogs(...)
    assertAuthorization(token, AuditLogRules.VIEW.resource());
    // Assert that parameter is alphanumeric or one of ". ?<>!="
    assertLegalSqlString(whereClause, true);
    // Assert that parameter is alphanumeric or one of ". "
    assertLegalSqlString(orderClause, false);
    // Start building the query
    final StringBuilder queryBuilder = new StringBuilder("SELECT a FROM ")
            .append(AuditRecordData.class.getSimpleName()).append(" a");
    // Optionally add the WHERE clause
    if (whereClause != null && whereClause.length() > 0) {
        queryBuilder.append(" WHERE ").append(whereClause);
    }
    // Optionally add the ORDER clause
    if (orderClause != null && orderClause.length() > 0) {
        queryBuilder.append(" ORDER BY ").append(orderClause);
    }
    final String queryString = queryBuilder.toString();
    if (LOG.isDebugEnabled()) {
        LOG.debug("queryString: " + queryString);
    }
    final Query query = entityManager.createQuery(queryString);
    query.setFirstResult(firstResult);
    if (maxResults > 0) {
        query.setMaxResults(maxResults);
    }
    if (parameters != null) {
        for (int i = 0; i < parameters.size(); i++) {
            query.setParameter(i, parameters.get(i));
        }
    }

    //Prune out result pertaining to unauthorized CAs
    Set<Integer> authorizedCaIds = new HashSet<>(caSession.getAuthorizedCaIds(token));
    List<AuditLogEntry> resultList = new ArrayList<>();
    for (AuditLogEntry auditLogEntry : (List<AuditLogEntry>) query.getResultList()) {
        //The following values may leak CA Ids. 
        if (auditLogEntry.getModuleTypeValue().equals(ModuleTypes.CA)
                || auditLogEntry.getModuleTypeValue().equals(ModuleTypes.CERTIFICATE)
                || auditLogEntry.getModuleTypeValue().equals(ModuleTypes.CRL)) {
            if (!StringUtils.isEmpty(auditLogEntry.getCustomId())) {
                if (!authorizedCaIds.contains(Integer.valueOf(auditLogEntry.getCustomId()))) {
                    continue;
                }
            }
        }
        resultList.add(auditLogEntry);
    }
    return resultList;

}

From source file:org.synyx.hades.dao.query.ParameterBinder.java

/**
 * Binds the parameters to the given query and applies special parameter
 * types (e.g. pagination).//from  w w  w.j a  v a 2 s  . c  om
 * 
 * @param query
 * @return
 */
public Query bindAndPrepare(Query query) {

    Query result = bind(query);

    if (!parameters.hasPageableParameter() || getPageable() == null) {
        return result;
    }

    result.setFirstResult(getPageable().getFirstItem());
    result.setMaxResults(getPageable().getPageSize());

    return result;
}

From source file:com.sun.socialsite.business.impl.JPAContentManagerImpl.java

/**
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 */// w w w . j a v a  2s. c o  m
@SuppressWarnings(value = "unchecked")
public List<Content> getUserContent(Profile profile, int offset, int length) throws SocialSiteException {
    if (profile == null) {
        throw new SocialSiteException("profile is null");
    }

    Query query = strategy.getNamedQuery("Content.getByUserName");
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    query.setParameter(1, profile);
    return (List<Content>) query.getResultList();
}

From source file:com.panguso.lc.analysis.format.dao.impl.DaoImpl.java

@Override
public List<T> find(int pageNo, int pageSize) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = cb.createQuery(entityClass);
    Root<T> customer = criteriaQuery.from(entityClass);
    criteriaQuery.getRoots().add(customer);
    Query query = em.createQuery(criteriaQuery);
    query.setFirstResult(pageNo * pageSize);
    query.setMaxResults(pageSize);
    return query.getResultList();
}

From source file:org.egov.ptis.domain.service.survey.SurveyApplicationService.java

public List<PropertyImpl> searchSurveyProperties(final SearchSurveyRequest searchSurveyRequest) {
    final StringBuilder queryStr = new StringBuilder(500);

    buildWhereClause(searchSurveyRequest, queryStr);
    final Query query = setSearchParameter(searchSurveyRequest, queryStr);
    query.setMaxResults(20);
    return query.getResultList();
}

From source file:com.mothsoft.alexis.dao.ModelDaoImpl.java

@SuppressWarnings("unchecked")
@Override//from ww w .j a va  2  s.  c o m
public DataRange<Model> listByOwner(Long userId, int first, int count) {
    final Query query = this.em
            .createQuery("SELECT m FROM Model m JOIN m.topic t WHERE t.userId = :userId ORDER BY m.name ASC");
    query.setParameter("userId", userId);
    query.setFirstResult(first);
    query.setMaxResults(count);

    final Query query2 = this.em
            .createQuery("SELECT COUNT(m.id) FROM Model m JOIN m.topic t WHERE t.userId = :userId");
    query2.setParameter("userId", userId);
    int total = ((Number) query2.getSingleResult()).intValue();

    final List<Model> list = query.getResultList();
    final DataRange<Model> range = new DataRange<Model>(list, first, total);
    return range;
}

From source file:org.oscarehr.common.dao.ConsultRequestDao.java

@SuppressWarnings("unchecked")
@Deprecated//from www  .jav  a 2  s. c  o m
public List<ConsultationRequest> listConsultationRequests(ConsultationQuery consultationQuery) {
    StringBuilder sql = this.generateQuery(consultationQuery, false);
    Query query = entityManager.createQuery(sql.toString());
    query.setFirstResult(consultationQuery.getStart());
    query.setMaxResults(consultationQuery.getLimit());
    return query.getResultList();
}

From source file:org.eurekastreams.server.persistence.JobMapper.java

/**
 * Look up company names starting with the given prefix.
 *
 * @param prefix/*from  w  ww .  j  a  va  2  s .  c  o m*/
 *            the company name prefix to search for
 * @param limit
 *            the maximum number of companies to return
 * @return the list of company names
 */
@SuppressWarnings("unchecked")
public List<String> findCompaniesByPrefix(final String prefix, final int limit) {
    String searchParam = prefix.toLowerCase() + "%";
    Query query = getEntityManager()
            .createQuery("select distinct(companyName) from Job j where lower(j.companyName) like :prefix "
                    + "order by companyName");
    query.setMaxResults(limit);
    query.setParameter("prefix", searchParam);

    return query.getResultList();
}

From source file:me.ronghai.sa.dao.impl.AbstractModelDAOWithEMImpl.java

@Override
public List<E> find(int from, int size, String configure) {
    String jpql = "SELECT e FROM " + entityClass.getName() + " e ";
    if (configure != null) {
        jpql = jpql + configure;//from   w  w  w  . jav  a  2  s.  c o  m
    }
    Query query = entityManager.createQuery(jpql);
    if (from != -1) {
        query.setFirstResult(from);
        query.setMaxResults(size);
    }
    return query.getResultList();
}

From source file:com.sun.socialsite.business.impl.JPAContentManagerImpl.java

/**
 * Get content by category and tag./*  w ww . j  a va  2s .  co  m*/
 *
 * Note: using SuppressWarnings annotation because the JPA API is not genericized.
 */
@SuppressWarnings(value = "unchecked")
public List<Content> getContentByCategory(Profile profile, String catscheme, String tag, int offset, int length)
        throws SocialSiteException {
    if (profile == null) {
        throw new SocialSiteException("profile is null");
    }

    if (catscheme == null) {
        throw new SocialSiteException("category scheme is null");
    }

    Query query = strategy.getNamedQuery("Content.getByProfileandCatscheme");
    if (offset != 0) {
        query.setFirstResult(offset);
    }
    if (length != -1) {
        query.setMaxResults(length);
    }
    query.setParameter(1, profile);
    query.setParameter(2, catscheme);
    return (List<Content>) query.getResultList();
}