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:fr.natoine.dao.annotation.DAOAnnotation.java

/**
 * Retrieves a specified quantity of annotations associated to a specified Resource through its URL, ordered by FirstAuthor
 * @param _url/*from   w  w w.j  a v a  2  s .  c  o  m*/
 * @param asc
 * @param first_indice
 * @param max_results
 * @return
 */
public List<Annotation> retrieveAnnotationsGroupByFirstAuthor(String _url, boolean asc, int first_indice,
        int max_results) {
    //EntityManagerFactory emf = this.setEMF();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    try {
        tx.begin();
        URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url)
                .getSingleResult();
        if (_uri == null) {
            tx.commit();
            //   //em.close();
            System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                    + " cause : there is no uri " + _url);
            return new ArrayList<Annotation>();
        }
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotated as annotated inner join annotated.representsResource as uri where uri=?").setParameter(1, _uri).getResultList();
        String order_by_clause = " annotation.id desc";
        if (asc)
            order_by_clause = " annotation.id asc";
        Query _query = em.createQuery(
                "select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? order by annotation.creator,"
                        + order_by_clause)
                .setParameter(1, _uri);
        _query.setFirstResult(first_indice);
        _query.setMaxResults(max_results);
        List<Annotation> annotations = _query.getResultList();
        //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? group by annotation.status" ).setParameter(1, _uri).getResultList();
        tx.commit();
        //em.close();
        return annotations;
    } catch (Exception e) {
        //tx.commit();
        tx.rollback();
        //em.close();
        System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations"
                + " cause : " + e.getMessage());
        return new ArrayList<Annotation>();
    }
}

From source file:br.org.indt.ndg.server.survey.SurveyHandlerBean.java

@Override
public QueryInputOutputVO listSurveysDB(NdgUser user, QueryInputOutputVO queryIOVO, Boolean isUploaded)
        throws MSMApplicationException, MSMSystemException {
    if (queryIOVO == null) {
        queryIOVO = new QueryInputOutputVO();
    }//from w ww  .j a va 2s. com

    try {
        MSMBusinessDelegate msmBD = new MSMBusinessDelegate();

        String sqlCommand = "SELECT U FROM Survey U WHERE idUser LIKE "
                + msmBD.findNdgUserByName(user.getUserAdmin()).getIdUser();

        if (isUploaded) {
            sqlCommand += " AND upper(U.isUploaded) <> 'N' ";
        }

        if ((queryIOVO.getFilterText() != null) && (queryIOVO.getFilterFields() != null)) {
            sqlCommand += SqlUtil.getFilterCondition(queryIOVO.getFilterText(), queryIOVO.getFilterFields());
        }

        if ((queryIOVO.getSortField() != null) && (queryIOVO.getIsDescending() != null)) {
            sqlCommand += SqlUtil.getSortCondition(queryIOVO.getSortField(), queryIOVO.getIsDescending());
        }

        Query q = manager.createQuery(sqlCommand);
        queryIOVO.setRecordCount(q.getResultList().size());

        if ((queryIOVO.getPageNumber() != null) && (queryIOVO.getRecordsPerPage() != null)) {
            q.setFirstResult((queryIOVO.getPageNumber() - 1) * queryIOVO.getRecordsPerPage());
            q.setMaxResults(queryIOVO.getRecordsPerPage());
        }

        ArrayList<Object> surveyList = new ArrayList<Object>();
        ArrayList<Survey> surveysListDB = (ArrayList<Survey>) q.getResultList();

        SurveyParser surveyParser = new SurveyParser();

        for (Survey survey : surveysListDB) {
            SurveyVO bean = new SurveyVO();

            SurveyXML surveyXml = surveyParser.parseSurvey(new StringBuffer(survey.getSurveyXml()), "UTF-8");

            /*
            surveyXml.setResults(loadResultsDB(surveyXml.getId()));
            */

            bean.setIdSurvey(surveyXml.getId());
            bean.setCheck("false");
            bean.setTitle(surveyXml.getTitle());
            bean.setResults(getQtResultsBySurvey(surveyXml.getId()));

            if ((survey.getIsUploaded() != 'n') && (survey.getIsUploaded() != 'N')) {
                bean.setIsUploaded('Y');
            } else {
                bean.setIsUploaded(survey.getIsUploaded());
            }

            if (!isUploaded) {
                bean.setSurvey(survey.getSurveyXml());
            }

            /*
            Collection<ImeiVO> devices = msmBD.getImeisSentBySurvey(surveyXml.getId());
            */

            Integer qtImeisSuccess = getQtImeisBySurvey(surveyXml.getId(), TransactionLogVO.STATUS_SUCCESS);
            bean.setDevice("" + qtImeisSuccess);

            /* 
            Collection<ImeiVO> pending = msmBD.getImeisUnsentBySurvey(surveyXml.getId());
            */

            Integer qtImeisPending = getQtImeisBySurvey(surveyXml.getId(), TransactionLogVO.STATUS_PENDING);
            bean.setPending("" + qtImeisPending);

            TransactionLogVO logs = msmBD.getSurveyReceived(surveyXml.getId());

            if (logs != null) {
                bean.setUser(logs.getUser());
                bean.setDate(SystemUtils.toDate(logs.getDtLog()));
            }

            surveyList.add(bean);

            log.info("Survey: " + surveyXml.getTitle());
        }

        queryIOVO.setQueryResult(surveyList);
    } catch (Exception e) {
        throw new SurveyNotParsedException();
    }

    return queryIOVO;
}

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

/**
 * @inheritDoc/*from   w w w . j a  va 2  s. c  om*/
 */
public List getComments(Weblog website, WeblogEntry entry, String searchString, Date startDate, Date endDate,
        String status, boolean reverseChrono, int offset, int length) throws WebloggerException {

    List params = new ArrayList();
    int size = 0;
    StringBuffer queryString = new StringBuffer();
    queryString.append("SELECT c FROM WeblogEntryComment c ");

    StringBuffer whereClause = new StringBuffer();
    if (entry != null) {
        params.add(size++, entry);
        whereClause.append("c.weblogEntry = ?").append(size);
    } else if (website != null) {
        params.add(size++, website);
        whereClause.append("c.weblogEntry.website = ?").append(size);
    }

    if (searchString != null) {
        params.add(size++, "%" + searchString + "%");
        appendConjuctionToWhereclause(whereClause, "(c.url LIKE ?").append(size).append(" OR c.content LIKE ?")
                .append(size).append(")");
    }

    if (startDate != null) {
        Timestamp start = new Timestamp(startDate.getTime());
        params.add(size++, start);
        appendConjuctionToWhereclause(whereClause, "c.postTime >= ?").append(size);
    }

    if (endDate != null) {
        Timestamp end = new Timestamp(endDate.getTime());
        params.add(size++, end);
        appendConjuctionToWhereclause(whereClause, "c.postTime <= ?").append(size);
    }

    if (status != null) {
        String comparisionOperator;
        if ("ALL_IGNORE_SPAM".equals(status)) {
            // we want all comments, except spam
            // so that means where status != SPAM
            status = WeblogEntryComment.SPAM;
            comparisionOperator = " <> ";
        } else {
            comparisionOperator = " = ";
        }
        params.add(size++, status);
        appendConjuctionToWhereclause(whereClause, "c.status ").append(comparisionOperator).append('?')
                .append(size);
    }

    if (whereClause.length() != 0) {
        queryString.append(" WHERE ").append(whereClause);
    }
    if (reverseChrono) {
        queryString.append(" ORDER BY c.postTime DESC");
    } else {
        queryString.append(" ORDER BY c.postTime ASC");
    }

    Query query = strategy.getDynamicQuery(queryString.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:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

/**
 * {@inheritDoc}//from w  w w  . java  2 s  .  c om
 */
@Override
public Page searchDocumentsRelatedVolume(String volumeToSearch, PaginationFilter paginationFilter)
        throws PersistenceException {
    Page page = new Page(paginationFilter);

    Query query = null;
    String toSearch = new String(
            "FROM Document WHERE volume.summaryId=" + volumeToSearch + " AND logicalDelete=false");

    if (paginationFilter.getTotal() == null) {
        String countQuery = "SELECT COUNT(*) " + toSearch;
        query = getEntityManager().createQuery(countQuery);
        page.setTotal(new Long((Long) query.getSingleResult()));
    }
    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

    query = getEntityManager().createQuery(toSearch + getOrderByQuery(paginationFilter.getSortingCriterias()));

    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());

    page.setList(query.getResultList());

    return page;
}

From source file:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

/**
 * {@inheritDoc}//from  w  w  w.  j  av  a 2s .  c o  m
 */
@Override
public Page searchSenderDocumentsPerson(String personToSearch, PaginationFilter paginationFilter)
        throws PersistenceException {
    Page page = new Page(paginationFilter);

    Query query = null;
    String toSearch = new String(
            "FROM Document WHERE senderPeople.personId=" + personToSearch + " AND logicalDelete=false");

    if (paginationFilter.getTotal() == null) {
        String countQuery = "SELECT COUNT(*) " + toSearch;
        query = getEntityManager().createQuery(countQuery);
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

    query = getEntityManager().createQuery(toSearch + getOrderByQuery(paginationFilter.getSortingCriterias()));

    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());

    page.setList(query.getResultList());

    return page;
}

From source file:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

/**
 * {@inheritDoc}/*from w  w  w  .  j  a  v  a2  s  .  c om*/
 */
@Override
public Page searchSenderDocumentsPlace(String placeToSearch, PaginationFilter paginationFilter)
        throws PersistenceException {
    Page page = new Page(paginationFilter);

    Query query = null;
    String toSearch = new String(
            "FROM Document WHERE senderPlace.placeAllId=" + placeToSearch + " AND logicalDelete=false");

    if (paginationFilter.getTotal() == null) {
        String countQuery = "SELECT COUNT(*) " + toSearch;
        query = getEntityManager().createQuery(countQuery);
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

    query = getEntityManager().createQuery(toSearch + getOrderByQuery(paginationFilter.getSortingCriterias()));

    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());

    page.setList(query.getResultList());

    return page;
}

From source file:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

/**
 * {@inheritDoc}/*from  ww  w .  j av  a 2  s. com*/
 */
@Override
public Page searchRecipientDocumentsPerson(String personToSearch, PaginationFilter paginationFilter)
        throws PersistenceException {
    Page page = new Page(paginationFilter);

    Query query = null;
    String toSearch = new String(
            "FROM Document WHERE recipientPeople.personId=" + personToSearch + " AND logicalDelete=false");

    if (paginationFilter.getTotal() == null) {
        String countQuery = "SELECT COUNT(*) " + toSearch;
        query = getEntityManager().createQuery(countQuery);
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

    query = getEntityManager().createQuery(toSearch + getOrderByQuery(paginationFilter.getSortingCriterias()));

    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());

    page.setList(query.getResultList());

    return page;
}

From source file:org.medici.bia.dao.document.DocumentDAOJpaImpl.java

/**
 * {@inheritDoc}/*w  w  w  .ja v a  2s.  c  om*/
 */
@Override
public Page searchRecipientDocumentsPlace(String placeToSearch, PaginationFilter paginationFilter)
        throws PersistenceException {
    Page page = new Page(paginationFilter);

    Query query = null;
    String toSearch = new String(
            "FROM Document WHERE recipientPlace.placeAllId=" + placeToSearch + " AND logicalDelete=false");

    if (paginationFilter.getTotal() == null) {
        String countQuery = "SELECT COUNT(*) " + toSearch;
        query = getEntityManager().createQuery(countQuery);
        page.setTotal(new Long((Long) query.getSingleResult()));
    }

    paginationFilter = generatePaginationFilterMYSQL(paginationFilter);

    query = getEntityManager().createQuery(toSearch + getOrderByQuery(paginationFilter.getSortingCriterias()));

    query.setFirstResult(paginationFilter.getFirstRecord());
    query.setMaxResults(paginationFilter.getLength());

    page.setList(query.getResultList());

    return page;
}

From source file:com.tzquery.fsn.dao.impl.TzQueryDaoImpl.java

/**
 * ?id????/*w w  w  .j  ava 2 s  . c  o m*/
 * @param paramVO
 * @param rId
 * @return
 * @throws DaoException
 */
@Override
public List<ProcurementUsageRecord> getProcurementUsageRecordList(TzQueryRequestParamVO paramVO, Long rId)
        throws DaoException {
    try {
        List<ProcurementUsageRecord> list = new ArrayList<ProcurementUsageRecord>();
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT * FROM procurement_usage_record WHERE procurement_id=?1 ORDER BY use_date desc");
        Query query = entityManager.createNativeQuery(sql.toString(), ProcurementUsageRecord.class);
        query.setParameter(1, rId);
        if (paramVO.getPage() > 0 && paramVO.getPageSize() > 0) {
            query.setFirstResult((paramVO.getPage() - 1) * paramVO.getPageSize());
            query.setMaxResults(paramVO.getPageSize());
        }
        list = query.getResultList();
        return list;
    } catch (Exception e) {
        throw new DaoException(
                "TzQueryDaoImpl-->getProcurementUsageRecordList()?id?????",
                e);
    }
}

From source file:com.tzquery.fsn.dao.impl.TzQueryDaoImpl.java

/**
 * ???????//from  w  ww.j  a  v a2  s .c om
 * @author ChenXiaolin 2015-12-04
 * @param paramVO
 * @return
 * @throws DaoException
 */
@SuppressWarnings("unchecked")
@Override
public List<TzQueryResponseBusVO> getAccountInfo(TzQueryRequestParamVO paramVO) throws DaoException {
    try {
        String changeSql = "SELECT DISTINCT outBus.id,outBus.name,outBus.license_no,outBus.type,outBus.address";
        StringBuffer sql = new StringBuffer();
        StringBuffer publicSql = new StringBuffer();
        publicSql.append(" FROM tz_business_account account");
        publicSql.append(" INNER JOIN business_unit inBus ON inBus.id = account.in_business_id");
        publicSql.append(" INNER JOIN business_unit outBus ON outBus.id = account.out_business_id");
        if (paramVO.getSalesType() == 0) {//
            sql.append(changeSql);
            sql.append(publicSql);
            sql.append(" WHERE inBus.id = ?1");
        } else if (paramVO.getSalesType() == 1) {//
            String temporarySql = changeSql.replace("outBus", "inBus");
            sql.append(temporarySql);
            sql.append(publicSql);
            sql.append(" WHERE outBus.id = ?1");
        }
        Query query = entityManager.createNativeQuery(sql.toString());
        query.setParameter(1, paramVO.getFirstBusId());
        if (paramVO.getPage() > 0 && paramVO.getPageSize() > 0) {
            query.setFirstResult((paramVO.getPage() - 1) * paramVO.getPageSize());
            query.setMaxResults(paramVO.getPageSize());
        }
        List<Object[]> objects = query.getResultList();
        return setAccounInfo(objects, paramVO);
    } catch (Exception e) {
        throw new DaoException(
                "TzQueryDaoImpl-->getAccountInfo()???????,?",
                e);
    }
}