Example usage for org.hibernate Query setFirstResult

List of usage examples for org.hibernate Query setFirstResult

Introduction

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

Prototype

@Override
    Query<R> setFirstResult(int startPosition);

Source Link

Usage

From source file:apm.common.core.DaoImpl.java

License:Open Source License

/**
 * QL /*from   ww  w. j a  va  2s .  c om*/
 * @param page
 * @param qlString
 * @param parameter
 * @return
 */
@SuppressWarnings("unchecked")
public <E> Page<E> find(Page<E> page, String qlString, Object... parameter) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        String countQlString = "select count(*) " + removeSelect(removeOrders(qlString));
        //           page.setCount(Long.valueOf(createQuery(countQlString, parameter).uniqueResult().toString()));
        Query query = createQuery(countQlString, parameter);
        List<Object> list = query.list();
        if (list.size() > 0) {
            page.setCount(Long.valueOf(list.get(0).toString()));
        } else {
            page.setCount(list.size());
        }
        if (page.getCount() < 1) {
            return page;
        }
    }
    // order by
    String ql = qlString;
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        ql += " order by " + page.getOrderBy();
    }
    Query query = createQuery(ql, parameter);
    // set page
    if (!page.isDisabled()) {
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResults());
    }
    page.setList(query.list());
    return page;
}

From source file:apm.common.core.DaoImpl.java

License:Open Source License

/**
* SQL /*from  w  w w  .j a  v a2s.  c o  m*/
* @param page
* @param sqlString
* @param resultClass
* @param parameter
* @return
*/
@SuppressWarnings("unchecked")
public <E> Page<E> findBySql(Page<E> page, String sqlString, Class<?> resultClass, Object... parameter) {
    // get count
    if (!page.isDisabled() && !page.isNotCount()) {
        String countSqlString = "select count(*) " + removeSelect(removeOrders(sqlString));
        //           page.setCount(Long.valueOf(createSqlQuery(countSqlString, parameter).uniqueResult().toString()));
        Query query = createSqlQuery(countSqlString, parameter);
        List<Object> list = query.list();
        if (list.size() > 0) {
            page.setCount(Long.valueOf(list.get(0).toString()));
        } else {
            page.setCount(list.size());
        }
        if (page.getCount() < 1) {
            return page;
        }
    }
    // order by
    String sql = sqlString;
    if (StringUtils.isNotBlank(page.getOrderBy())) {
        sql += " order by " + page.getOrderBy();
    }
    SQLQuery query = createSqlQuery(sql, parameter);
    // set page
    if (!page.isDisabled()) {
        query.setFirstResult(page.getFirstResult());
        query.setMaxResults(page.getMaxResults());
    }
    setResultTransformer(query, resultClass);
    page.setList(query.list());
    return page;
}

From source file:at.molindo.esi4j.module.hibernate.scrolling.CustomQueryScrollingSession.java

License:Apache License

private List<?> fetch(Query query, int first, int max) {
    // TODO there are better ways to scroll than setFirstResult(..)
    return query.setFirstResult(first).setMaxResults(max).setCacheable(false).list();
}

From source file:at.treedb.db.hibernate.DAOhibernate.java

License:Open Source License

@Override
public List<?> query(String query, int start, int length, HashMap<String, Object> map) {
    Query q;
    if (isStatelessSession) {
        q = statelessSession.createQuery(query);
    } else {//from ww  w  .ja v a  2s. c  o m
        q = session.createQuery(query);
    }
    if (map != null) {
        Iterator<String> iter = map.keySet().iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            Object o = map.get(key);
            q.setParameter(key, o);
        }
    }
    q.setFirstResult(start);
    q.setMaxResults(length);
    return q.list();
}

From source file:au.org.theark.lims.model.dao.BioCollectionDao.java

License:Open Source License

public List<BioCollectionCustomFieldData> getBioCollectionCustomFieldDataList(
        BioCollection bioCollectionCriteria, ArkFunction arkFunction, int first, int count) {
    List<BioCollectionCustomFieldData> bioCollectionCustomFieldDataList = new ArrayList<BioCollectionCustomFieldData>();

    StringBuffer sb = new StringBuffer();
    sb.append(" FROM  CustomFieldDisplay AS cfd ");
    sb.append("LEFT JOIN cfd.bioCollectionCustomFieldData as fieldList ");
    sb.append(" with fieldList.bioCollection.id = :bioCollectionId ");
    sb.append("  WHERE cfd.customField.study.id IN (:studyId)");
    sb.append(" AND cfd.customField.arkFunction.id = :functionId");
    sb.append(" ORDER BY cfd.sequence");

    Query query = getSession().createQuery(sb.toString());
    query.setParameter("bioCollectionId", bioCollectionCriteria.getId());

    // Allow child studies to inherit parent defined custom fields
    List studyList = new ArrayList();
    studyList.add(bioCollectionCriteria.getStudy().getId());
    if (bioCollectionCriteria.getStudy().getParentStudy() != null
            && bioCollectionCriteria.getStudy().getParentStudy() != bioCollectionCriteria.getStudy()) {
        studyList.add(bioCollectionCriteria.getStudy().getParentStudy().getId());
    }/*from w  w  w .  ja  v a  2 s .  c o  m*/
    query.setParameterList("studyId", studyList);
    query.setParameter("functionId", arkFunction.getId());
    query.setFirstResult(first);
    query.setMaxResults(count);

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

            cfd = (CustomFieldDisplay) objects[0];
            if (objects[1] != null) {
                bccfd = (BioCollectionCustomFieldData) objects[1];
            } else {
                bccfd.setCustomFieldDisplay(cfd);
            }
            bioCollectionCustomFieldDataList.add(bccfd);
        }
    }
    return bioCollectionCustomFieldDataList;
}

From source file:au.org.theark.lims.model.dao.BiospecimenDao.java

License:Open Source License

public List<BiospecimenCustomFieldData> getBiospecimenCustomFieldDataList(Biospecimen biospecimenCriteria,
        ArkFunction arkFunction, int first, int count) {
    List<BiospecimenCustomFieldData> biospecimenCustomFieldDataList = new ArrayList<BiospecimenCustomFieldData>();

    StringBuffer sb = new StringBuffer();
    sb.append(" FROM  CustomFieldDisplay AS cfd ");
    sb.append("LEFT JOIN cfd.biospecimenCustomFieldData as fieldList ");
    sb.append(" with fieldList.biospecimen.id = :biospecimenId ");
    sb.append("  where cfd.customField.study.id IN (:studyId)");
    sb.append(" and cfd.customField.arkFunction.id = :functionId");
    sb.append(" order by cfd.sequence");

    Query query = getSession().createQuery(sb.toString());
    query.setParameter("biospecimenId", biospecimenCriteria.getId());

    // Allow child studies to inherit parent defined custom fields
    List studyList = new ArrayList();
    studyList.add(biospecimenCriteria.getStudy().getId());
    if (biospecimenCriteria.getStudy().getParentStudy() != null
            && biospecimenCriteria.getStudy().getParentStudy() != biospecimenCriteria.getStudy()) {
        studyList.add(biospecimenCriteria.getStudy().getParentStudy().getId());
    }/*from   w ww .  j  a  v a2s.c o  m*/
    query.setParameterList("studyId", studyList);
    query.setParameter("functionId", arkFunction.getId());
    query.setFirstResult(first);
    query.setMaxResults(count);

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

            cfd = (CustomFieldDisplay) objects[0];
            if (objects[1] != null) {
                bscfd = (BiospecimenCustomFieldData) objects[1];
            } else {
                bscfd.setCustomFieldDisplay(cfd);
            }
            biospecimenCustomFieldDataList.add(bscfd);
        }
    }
    return biospecimenCustomFieldDataList;
}

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

License:Open Source License

public List<PhenoDataSetData> getPhenoDataList(PhenoDataSetCollection phenoCollection,
        PhenoDataSetCategory phenoDataSetCategory, int first, int count) {

    List<PhenoDataSetData> phenoDataList = new ArrayList<PhenoDataSetData>();

    // The following HQL is based on this SQL, except that we don't need the CustomField
    //      SELECT *
    //        FROM study.custom_field cf
    //       INNER JOIN study.custom_field_display cfd
    //          ON cfd.custom_field_id = cf.id
    //       INNER JOIN study.custom_field_group cfg
    //          ON cfd.custom_field_group_id = cfg.id
    //       INNER JOIN pheno.pheno_collection pc
    //          ON pc.custom_field_group_id = cfg.id
    //        LEFT JOIN pheno.pheno_data pd
    //          ON pd.custom_field_display_id = cfd.id
    //         AND pd.pheno_collection_id = pc.id
    //       WHERE pc.id = 1;

    /*      // w ww.  jav a  2  s  .  c om
     * WARNING: Do no try to do a HQL WITH clause between "pd.phenoCollection.id" and another table's column!
     * Even though it looks like it should work in SQL (as above), for an unknown reason HQL parsing will fail - like this:
     * ----
     * Caused by: org.hibernate.hql.ast.QuerySyntaxException: with-clause referenced two different from-clause elements 
     * [  FROM au.org.theark.core.model.study.entity.CustomFieldDisplay AS cfd  
     * INNER JOIN cfd.customFieldGroup cfg  
     * INNER JOIN cfg.phenoCollection pc   
     * LEFT JOIN cfd.phenoData AS pd   
     * WITH pd.phenoCollection.id = pc.id 
     * WHERE pc.id = :pcId ORDER BY cfd.sequence ]
     * ----
     * Thus the present work-around is to use an argument "pcId" for the WITH criteria.
     */
    StringBuffer sb = new StringBuffer();
    sb.append("SELECT pdsfd, pdsd ");
    sb.append("  FROM PhenoDataSetFieldDisplay AS pdsfd ");
    sb.append(" INNER JOIN pdsfd.phenoDataSetGroup AS pdsg ");
    sb.append(" INNER JOIN pdsg.phenoDataSetCollections pdsc ");
    sb.append("  LEFT JOIN pdsfd.phenoDataSetData AS pdsd ");
    sb.append("  WITH pdsd.phenoDataSetCollection.id = :pcId ");
    sb.append(" WHERE pdsc.id = :pcId ");
    sb.append(" and pdsfd.phenoDataSetCategory = :phenoDataSetCategory ");
    sb.append(" and pdsfd.phenoDataSetField is not null ");
    sb.append(" ORDER BY pdsfd.phenoDataSetFiledOrderNumber ");

    Query query = getSession().createQuery(sb.toString());
    query.setParameter("pcId", phenoCollection.getId());
    query.setParameter("phenoDataSetCategory", phenoDataSetCategory);
    query.setFirstResult(first);
    query.setMaxResults(count);

    List<Object[]> listOfObjects = query.list();
    for (Object[] objects : listOfObjects) {
        PhenoDataSetFieldDisplay pfd = new PhenoDataSetFieldDisplay();
        PhenoDataSetData phenoData = new PhenoDataSetData();
        if (objects.length > 0 && objects.length >= 1) {
            pfd = (PhenoDataSetFieldDisplay) objects[0];
            if (objects[1] != null) {
                phenoData = (PhenoDataSetData) objects[1];
            } else {
                phenoData.setPhenoDataSetFieldDisplay(pfd);
            }
            phenoDataList.add(phenoData);
        }
    }
    return phenoDataList;
}

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);
    query.setMaxResults(count);/*ww w  .  j ava  2s .  c o  m*/

    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  ww  w  .  ja v  a 2s  .  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>//w  w  w  . j av a2s .  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<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;
}