Example usage for org.hibernate Query setMaxResults

List of usage examples for org.hibernate Query setMaxResults

Introduction

In this page you can find the example usage for org.hibernate Query setMaxResults.

Prototype

@Override
    Query<R> setMaxResults(int maxResult);

Source Link

Usage

From source file:au.org.theark.phenotypic.model.dao.PhenotypicDao.java

License:Open Source License

public List<PhenoDataSetCollection> searchPageablePhenoCollection(PhenoDataCollectionVO collectionCriteria,
        int first, int count) {

    List<PhenoDataSetCollection> resultList = new ArrayList<PhenoDataSetCollection>();
    StringBuffer sb = new StringBuffer();
    sb.append("SELECT qnaire, pc ");
    sb.append("  FROM " + PhenoDataSetGroup.class.getName() + " AS qnaire ");
    sb.append("  LEFT JOIN qnaire.phenoDataSetCollections as pc ");
    sb.append("  WITH pc.linkSubjectStudy.id = :subjectId ");
    sb.append(" WHERE qnaire.study.id = :studyId ");
    //sb.append("   AND qnaire.arkFunction.id = :functionId ");
    sb.append("   AND qnaire.published = true ");

    Query query = getSession().createQuery(sb.toString());
    query.setParameter("subjectId",
            collectionCriteria.getPhenoDataSetCollection().getLinkSubjectStudy().getId());
    query.setParameter("studyId", collectionCriteria.getPhenoDataSetGroup().getStudy().getId());
    //log.info("colcrit ark=" + collectionCriteria.getArkFunction());
    //long id = collectionCriteria.getArkFunction().getId();
    //log.info("id=" + id);
    //query.setParameter("functionId",id);
    query.setFirstResult(first);/*  w w w. j  a v a  2 s  . c  o m*/
    query.setMaxResults(count);

    List<Object[]> listOfObjects = query.list();
    for (Object[] objects : listOfObjects) {
        //CustomFieldGroup questionnaire = new CustomFieldGroup();
        PhenoDataSetGroup questionnaire = new PhenoDataSetGroup();
        PhenoDataSetCollection pc = new PhenoDataSetCollection();
        if (objects.length > 0 && objects.length >= 1) {
            questionnaire = (PhenoDataSetGroup) objects[0];
            if (objects[1] != null) {
                pc = (PhenoDataSetCollection) objects[1];
            } else {
                pc.setQuestionnaire(questionnaire);
            }
            resultList.add(pc);
        }
    }
    Criteria criteria = getSession().createCriteria(PhenoDataSetCollection.class);
    criteria.createAlias("questionnaire", "qnaire");
    criteria.add(Restrictions.eq("linkSubjectStudy",
            collectionCriteria.getPhenoDataSetCollection().getLinkSubjectStudy()));
    // Just a precaution (PhenoCollection to should always map to a CustomFieldGroup where the ArkFunction will correspond to Pheno) 
    criteria.add(Restrictions.eq("qnaire.arkFunction", collectionCriteria.getArkFunction()));
    criteria.setFirstResult(first);
    criteria.setMaxResults(count);
    resultList = criteria.list();
    return resultList;
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

/**
 * <p>//from   w w w .j  a  va 2 s. c  o  m
 * Builds a HQL to Left Join wtih SubjectCustomFieldData and applies a condition using the WITH clause to get a sub-set for the given Subject and
 * then applies the restrictions on study and module.
 * </p>
 */
public List<SubjectCustomFieldData> getSubjectCustomFieldDataList(LinkSubjectStudy linkSubjectStudyCriteria,
        ArkFunction arkFunction, CustomFieldCategory customFieldCategory, CustomFieldType customFieldType,
        int first, int count) {

    List<SubjectCustomFieldData> subjectCustomFieldDataList = new ArrayList<SubjectCustomFieldData>();

    StringBuffer sb = new StringBuffer();
    sb.append("SELECT cfd, fieldList");
    sb.append(" FROM  CustomFieldDisplay AS cfd ");
    sb.append("LEFT JOIN cfd.customField AS cf ");
    sb.append("LEFT JOIN cf.customFieldType AS cft ");
    sb.append("LEFT JOIN cfd.subjectCustomFieldData as fieldList ");
    sb.append(" with fieldList.linkSubjectStudy.id = :subjectId ");
    sb.append("  where cfd.customField.study.id = :studyId");
    sb.append(" and cfd.customField.arkFunction.id = :functionId");
    //Add new requirement for the category
    if (customFieldCategory != null) {
        sb.append(" and cfd.customField.customFieldCategory.id = :customFieldCategotyId");
    }
    //      if(type == null || "SUBJECT".equalsIgnoreCase(type)){
    sb.append(" and (cft is null or cft.name = :type)");
    //      }else{
    //         sb.append(" and cft.name = :type");
    //      }
    sb.append(" order by cfd.sequence");

    Query query = getSession().createQuery(sb.toString());
    query.setParameter("subjectId", linkSubjectStudyCriteria.getId());
    query.setParameter("studyId", linkSubjectStudyCriteria.getStudy().getId());
    query.setParameter("functionId", arkFunction.getId());
    //Add type and category
    if (customFieldCategory != null) {
        query.setParameter("customFieldCategotyId", customFieldCategory.getId());
    }
    query.setParameter("type", customFieldType.getName());
    query.setFirstResult(first);
    query.setMaxResults(count);

    List<Object[]> listOfObjects = query.list();
    for (Object[] objects : listOfObjects) {
        CustomFieldDisplay cfd = new CustomFieldDisplay();
        SubjectCustomFieldData scfd = new SubjectCustomFieldData();
        if (objects.length > 0 && objects.length >= 1) {

            cfd = (CustomFieldDisplay) objects[0];
            if (objects[1] != null) {
                scfd = (SubjectCustomFieldData) objects[1];
            } else {
                scfd.setCustomFieldDisplay(cfd);
            }

            subjectCustomFieldDataList.add(scfd);
        }
    }
    return subjectCustomFieldDataList;
}

From source file:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

/**
 * <p>/*from   w  w  w.  j  a v  a2s.co m*/
 * Builds a HQL to Left Join wtih SubjectCustomFieldData and applies a condition using the WITH clause to get a sub-set for the given Subject and
 * then applies the restrictions on study and module.
 * </p>
 */
public List<FamilyCustomFieldData> getFamilyCustomFieldDataList(LinkSubjectStudy linkSubjectStudyCriteria,
        ArkFunction arkFunction, CustomFieldCategory customFieldCategory, CustomFieldType customFieldType,
        int first, int count) {

    List<FamilyCustomFieldData> familyCustomFieldDataList = new ArrayList<FamilyCustomFieldData>();

    StringBuffer sb = new StringBuffer();

    sb.append("SELECT cfd, fieldList");
    sb.append(" FROM  CustomFieldDisplay AS cfd ");
    sb.append("LEFT JOIN cfd.customField AS cf ");
    sb.append("LEFT JOIN cf.customFieldType AS cft ");
    sb.append("LEFT JOIN cfd.familyCustomFieldData as fieldList ");
    sb.append(" with fieldList.familyUid = :familyUid ");
    sb.append("  where cfd.customField.study.id = :studyId");
    sb.append(" and cfd.customField.arkFunction.id = :functionId");
    //Add new requirement for the category
    if (customFieldCategory != null) {
        sb.append(" and cfd.customField.customFieldCategory.id = :customFieldCategotyId");
    }
    //      if(type == null || "SUBJECT".equalsIgnoreCase(type)){
    //         sb.append(" and (cft is null or cft.name = :type)");
    //      }else{
    sb.append(" and cft.name = :type");
    //      }
    sb.append(" order by cfd.sequence");

    Query query = getSession().createQuery(sb.toString());
    query.setParameter("familyUid", getSubjectFamilyUId(linkSubjectStudyCriteria.getStudy().getId(),
            linkSubjectStudyCriteria.getSubjectUID()));
    query.setParameter("studyId", linkSubjectStudyCriteria.getStudy().getId());
    query.setParameter("functionId", arkFunction.getId());
    //Add type and category
    if (customFieldCategory != null) {
        query.setParameter("customFieldCategotyId", customFieldCategory.getId());
    }
    query.setParameter("type", customFieldType.getName());
    query.setFirstResult(first);
    query.setMaxResults(count);

    List<Object[]> listOfObjects = query.list();
    for (Object[] objects : listOfObjects) {
        CustomFieldDisplay cfd = new CustomFieldDisplay();
        FamilyCustomFieldData fcfd = new FamilyCustomFieldData();
        if (objects.length > 0 && objects.length >= 1) {

            cfd = (CustomFieldDisplay) objects[0];
            if (objects[1] != null) {
                fcfd = (FamilyCustomFieldData) objects[1];
            } else {
                fcfd.setCustomFieldDisplay(cfd);
            }

            familyCustomFieldDataList.add(fcfd);
        }
    }
    return familyCustomFieldDataList;
}

From source file:br.com.gvt.eng.vod.dao.AssetDAO.java

public List<OnDemandContentVO> findTop50() {

    StringBuilder aux = new StringBuilder(
            "select a.assetId, count(*) as total from IpvodPurchase p join p.ipvodAsset a group by a.assetId order by total desc");

    Query query = getSession().createQuery(String.valueOf(aux));
    query.setFirstResult(0);/*from   ww w .  j ava2 s  . c  o  m*/
    query.setMaxResults(50);

    @SuppressWarnings("unchecked")
    List<Object[]> list = query.list();

    List<OnDemandContentVO> vos = new ArrayList<OnDemandContentVO>();

    for (Object[] obj : list) {

        StringBuilder hql = new StringBuilder("select ");
        hql.append("a.title as title, ");
        hql.append("a.originalTitle as originalTitle, ");
        hql.append("'UNKNOWN' as genre, ");
        hql.append("c.description as category, ");
        hql.append("a.subtitles as subtitle, ");
        hql.append("a.country as country, ");
        hql.append("sc.description as subCategory, ");
        hql.append("a.assetId as assetId, ");
        hql.append("a.creationDate as creationDate, ");
        hql.append("a.description as description, ");
        hql.append("a.director as director, ");
        hql.append("a.actors as actors, ");
        hql.append("a.episode as episode, ");
        hql.append("a.billingID as billingID, ");
        hql.append("a.episodeName as episodeName, ");
        hql.append("a.licenseWindowEnd as licenseWindowEnd, ");
        hql.append("a.licenseWindowStart as licenseWindowStart, ");
        hql.append("a.price as price, ");
        hql.append("a.releaseYear as releaseYear, ");
        hql.append("a.season as season, ");
        hql.append("a.languages as languages, ");
        hql.append("a.assetInfo as assetInfo, ");
        hql.append("r.rating as rating, ");
        hql.append("r.adult as isAdult, ");
        hql.append("a.totalTime as totalTime, ");
        hql.append("a.product as product, ");
        hql.append("a.screenFormat as screenFormat, ");
        hql.append("a.audioType as audioType, ");
        hql.append("a.canResume as canResume, ");
        hql.append("a.isHD as isHD, ");
        hql.append("a.isRevised as isRevised, ");
        hql.append("a.fileSize as fileSize, ");
        hql.append("a.checksum as checksum, ");
        hql.append("a.bitrate as bitrate, ");
        hql.append("a.titleAlternative as titleAlternative, ");
        hql.append("at.description as assetType, ");
        hql.append("cp.providerId as contentProvider ");
        hql.append(
                "from IpvodAsset as a left join a.ipvodCategory1 c left join a.ipvodCategory2 sc left join a.ipvodAssetType at left join a.ipvodContentProvider cp left join a.rating r ");
        hql.append("where a.assetId = :assetId order by a.title asc ");

        query = getSession().createQuery(String.valueOf(hql));
        query.setParameter("assetId", obj[0]);
        query.setResultTransformer(new AliasToBeanResultTransformer(OnDemandContentVO.class));

        OnDemandContentVO vo = (OnDemandContentVO) query.uniqueResult();
        vo.setOrder(new Long(list.lastIndexOf(obj) + 1));

        vos.add(list.lastIndexOf(obj), vo);

    }

    return vos;
}

From source file:br.com.gvt.eng.vod.dao.IngestDAO.java

/**
 * @param query/*from w w  w.  ja  v  a2  s.c o m*/
 * @param uriInfo
 */
public void setPagination(Query query, UriInfo uriInfo) {

    if (uriInfo.getQueryParameters().getFirst(IpvodConstants.URLPARAM_PAGE_NUMBER) != null) {
        int pageNumber = Integer
                .parseInt((String) uriInfo.getQueryParameters().getFirst(IpvodConstants.URLPARAM_PAGE_NUMBER));

        int maxResults = IpvodConstants.REGISTERS_PER_PAGE;
        if (uriInfo.getQueryParameters().getFirst(IpvodConstants.URLPARAM_REGISTER_PER_PAGE) != null) {
            maxResults = Integer.parseInt(
                    (String) uriInfo.getQueryParameters().getFirst(IpvodConstants.URLPARAM_REGISTER_PER_PAGE));
        }

        query.setMaxResults(maxResults);
        query.setFirstResult((pageNumber * maxResults) - maxResults);

    }

    if (uriInfo.getQueryParameters().getFirst(IpvodConstants.URLPARAM_FIRST_INDEX) != null) {
        int firstResult = Integer
                .parseInt((String) uriInfo.getQueryParameters().getFirst(IpvodConstants.URLPARAM_FIRST_INDEX));

        int maxResults = IpvodConstants.REGISTERS_PER_PAGE;
        if (uriInfo.getQueryParameters().getFirst(IpvodConstants.URLPARAM_REGISTER_PER_PAGE) != null) {
            maxResults = Integer.parseInt(
                    (String) uriInfo.getQueryParameters().getFirst(IpvodConstants.URLPARAM_REGISTER_PER_PAGE));
        }

        query.setFirstResult(firstResult);
        query.setMaxResults(maxResults);
    }
}

From source file:br.com.sescacre.dao.PessoasDAO.java

public List<Pessoas> pesquisaPessoaPorNome(String nome) {
    Session s = HibernateUtil.getSession();
    Query q = s.createQuery("from Pessoas p where p.nome like :nome");
    q.setParameter("nome", nome + "%");
    q.setMaxResults(5);
    List<Pessoas> lista = q.list();
    s.close();/*  ww w. j a  v a  2 s . c o  m*/
    return lista;
}

From source file:br.com.suricattus.surispring.hibernate.criterion.HQL.java

License:Open Source License

@SuppressWarnings("rawtypes")
private Query getQuery() {

    StringBuilder hql = new StringBuilder();

    if (!select.isEmpty())
        append(hql, "select", select, ",");
    append(hql, "from", from);
    if (!where.isEmpty())
        append(hql, "where", where, "and");
    if (!group.isEmpty())
        append(hql, "group by", group, ",");
    if (!order.isEmpty())
        append(hql, "order by", order, ",");

    Query query = session.createQuery(hql.toString());

    for (String paramName : params.keySet()) {
        Object param = params.get(paramName);
        if (param instanceof Collection) {
            query.setParameterList(paramName, (Collection) params.get(paramName));
        } else if (param instanceof Object[]) {
            query.setParameterList(paramName, (Object[]) params.get(paramName));
        } else {// w  w w .  j  a v  a2  s.c o m
            query.setParameter(paramName, params.get(paramName));
        }
    }

    if (firstResult != null)
        query.setFirstResult(firstResult);
    if (maxResults != null)
        query.setMaxResults(maxResults);

    return query;
}

From source file:br.gov.jfrj.siga.dp.dao.CpDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<CpOrgao> consultarPorFiltro(final CpOrgaoDaoFiltro o, final int offset, final int itemPagina) {
    try {/*from  w w w . ja  v a2  s.  c  o m*/
        final Query query = getSessao().getNamedQuery("consultarPorFiltroCpOrgao");
        if (offset > 0) {
            query.setFirstResult(offset);
        }
        if (itemPagina > 0) {
            query.setMaxResults(itemPagina);
        }
        String s = o.getNome();
        if (s != null)
            s = s.replace(' ', '%');
        query.setString("nome", s);

        final List<CpOrgao> l = query.list();
        return l;
    } catch (final NullPointerException e) {
        return null;
    }
}

From source file:br.gov.jfrj.siga.dp.dao.CpDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<CpOrgaoUsuario> consultarPorFiltro(final CpOrgaoUsuarioDaoFiltro o, final int offset,
        final int itemPagina) {
    try {//from  w w w .  j av a  2  s .  c o m
        final Query query = getSessao().getNamedQuery("consultarPorFiltroCpOrgao");
        if (offset > 0) {
            query.setFirstResult(offset);
        }
        if (itemPagina > 0) {
            query.setMaxResults(itemPagina);
        }
        String s = o.getNome();
        if (s != null)
            s = s.replace(' ', '%');
        query.setString("nome", s);

        query.setCacheable(true);
        query.setCacheRegion(CACHE_QUERY_HOURS);

        final List<CpOrgaoUsuario> l = query.list();
        return l;
    } catch (final NullPointerException e) {
        return null;
    }
}

From source file:br.gov.jfrj.siga.dp.dao.CpDao.java

License:Open Source License

@SuppressWarnings("unchecked")
public List<DpCargo> consultarPorFiltro(final DpCargoDaoFiltro o, final int offset, final int itemPagina) {
    try {//  ww w . j  a  va2  s  .  c o m
        final Query query = getSessao().getNamedQuery("consultarPorFiltroDpCargo");
        if (offset > 0) {
            query.setFirstResult(offset);
        }
        if (itemPagina > 0) {
            query.setMaxResults(itemPagina);
        }
        String s = o.getNome();
        if (s != null)
            s = s.replace(' ', '%');
        query.setString("nome", s);

        if (o.getIdOrgaoUsu() != null)
            query.setLong("idOrgaoUsu", o.getIdOrgaoUsu());
        else
            query.setLong("idOrgaoUsu", 0);

        final List<DpCargo> l = query.list();
        return l;
    } catch (final NullPointerException e) {
        return null;
    }
}