Example usage for org.hibernate Query setParameter

List of usage examples for org.hibernate Query setParameter

Introduction

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

Prototype

@SuppressWarnings("unchecked")
Query<R> setParameter(int position, Object val);

Source Link

Document

Bind a positional query parameter using its inferred Type.

Usage

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

License:Open Source License

/**
 * Ensures that the lastname in the db is returned
 *///from w w  w.j  ava 2  s  .  c om
public String getCurrentLastnameFromDB(Person person) {
    /*
     * Criteria criteria = getSession().createCriteria(PersonLastnameHistory.class);
     * 
     * if (person.getId() != null) { criteria.add(Restrictions.eq(au.org.theark.core.Constants.PERSON_SURNAME_HISTORY_PERSON, person)); }
     * criteria.addOrder(Order.desc("id")); PersonLastnameHistory personLastnameHistory = new PersonLastnameHistory(); if
     * (!criteria.list().isEmpty()) { personLastnameHistory = (PersonLastnameHistory) criteria.list().get(0); }
     * 
     * return personLastnameHistory.getLastName();
     */

    if (person.getId() != null) {
        Query q = getSession()
                .createQuery("Select person.lastName from Person person where person.id = :personId ");
        q.setParameter("personId", person.getId());

        List<String> results = q.list();
        if (!results.isEmpty()) {
            return (String) results.get(0);
        }
    }

    return null;
}

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

License:Open Source License

/**
 * <p>//  ww w  .  j  a v a 2  s. c om
 * 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 ww w.  j  a  va  2s  .com*/
 * 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:au.org.theark.study.model.dao.StudyDao.java

License:Open Source License

@Override
public String getSubjectFamilyUId(Long studyId, String subjectUID) {
    String result = null;/*from  w w w  .  j a  v  a  2s. c o m*/

    StringBuffer sb = new StringBuffer("select scfd.TEXT_DATA_VALUE from " + "study.link_subject_study lss "
            + "left outer join study.study st on lss.STUDY_ID = st.ID "
            + "left outer join study.study_pedigree_config spc on spc.study_id = st.ID "
            + "left outer join study.custom_field cf on cf.ID = spc.family_id "
            + "left outer join study.custom_field_display cfd on cfd.custom_field_id = cf.id "
            + "left outer join study.subject_custom_field_data scfd on scfd.CUSTOM_FIELD_DISPLAY_ID = cfd.id "
            + "and scfd.LINK_SUBJECT_STUDY_ID = lss.ID "
            + "where st.ID= :studyId and lss.SUBJECT_UID = :subjectUID and spc.family_id is not null");

    Query query = getSession().createSQLQuery(sb.toString());
    query.setParameter("studyId", studyId);
    query.setParameter("subjectUID", subjectUID);
    result = (String) query.uniqueResult();
    return result;
}

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

License:Open Source License

public Long isCustomFieldUsed(SubjectCustomFieldData subjectCustomFieldData) {
    Long count = new Long("0");
    CustomField customField = subjectCustomFieldData.getCustomFieldDisplay().getCustomField();

    Study study = customField.getStudy();
    ArkFunction arkFunction = customField.getArkFunction();

    StringBuffer stringBuffer = new StringBuffer();

    stringBuffer.append(" SELECT COUNT(*) FROM SubjectCustomFieldData AS scfd WHERE EXISTS ");
    stringBuffer.append(" ( ");
    stringBuffer.append(// ww  w.j a  v  a  2 s .  c  om
            " SELECT cfd.id FROM  CustomFieldDisplay AS cfd  WHERE cfd.customField.study.id = :studyId");
    stringBuffer.append(
            " AND cfd.customField.arkFunction.id = :functionId AND scfd.customFieldDisplay.id = :customFieldDisplayId");
    stringBuffer.append(" )");

    String theHQLQuery = stringBuffer.toString();

    Query query = getSession().createQuery(theHQLQuery);
    query.setParameter("studyId", study.getId());
    query.setParameter("functionId", arkFunction.getId());
    query.setParameter("customFieldDisplayId", subjectCustomFieldData.getCustomFieldDisplay().getId());
    count = (Long) query.uniqueResult();

    return count;
}

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

License:Open Source License

public Long isFamilyCustomFieldUsed(FamilyCustomFieldData familyCustomFieldData) {
    Long count = new Long("0");
    CustomField customField = familyCustomFieldData.getCustomFieldDisplay().getCustomField();

    Study study = customField.getStudy();
    ArkFunction arkFunction = customField.getArkFunction();

    StringBuffer stringBuffer = new StringBuffer();

    stringBuffer.append(" SELECT COUNT(*) FROM FamilyCustomFieldData AS scfd WHERE EXISTS ");
    stringBuffer.append(" ( ");
    stringBuffer.append(/*  w w w .ja v a  2  s  .  c o  m*/
            " SELECT cfd.id FROM  CustomFieldDisplay AS cfd  WHERE cfd.customField.study.id = :studyId");
    stringBuffer.append(
            " AND cfd.customField.arkFunction.id = :functionId AND scfd.customFieldDisplay.id = :customFieldDisplayId");
    stringBuffer.append(" )");

    String theHQLQuery = stringBuffer.toString();

    Query query = getSession().createQuery(theHQLQuery);
    query.setParameter("studyId", study.getId());
    query.setParameter("functionId", arkFunction.getId());
    query.setParameter("customFieldDisplayId", familyCustomFieldData.getCustomFieldDisplay().getId());
    count = (Long) query.uniqueResult();

    return count;
}

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

License:Open Source License

public void setPreferredMailingAdressToFalse(Person person) {
    String queryString = "UPDATE Address SET preferredMailingAddress = 0 WHERE person = :person";
    Query query = getSession().createQuery(queryString);
    query.setParameter("person", person);
    query.executeUpdate();//from w w  w. ja v a 2 s .c  o  m
}

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

License:Open Source License

@Override
public void setPreferredPhoneNumberToFalse(Person person) {
    String queryString = "UPDATE Phone SET preferredPhoneNumber = 0 WHERE person = :person";
    Query query = getSession().createQuery(queryString);
    query.setParameter("person", person);
    query.executeUpdate();/*from  www  .  j a v  a2 s .com*/
}

From source file:automatedbillingsoftware_DA.Categories_DA.java

public List<Categories> fetchCategorieses() {
    SessionFactory sessionFactory = HibernateUtils.getLocSessionFactory();
    Session session = sessionFactory.getCurrentSession();
    Transaction beginTransaction = session.beginTransaction();
    Query query = session.createQuery("from Categories c where c.status=:status");
    query.setParameter("status", 1);
    List<Categories> catList = (List<Categories>) query.list();
    beginTransaction.commit();//from ww w  . j a v a2  s  .  c om
    return catList;
}

From source file:automatedbillingsoftware_DA.Categories_DA.java

public Categories fetchCategoryByName(String name) {
    SessionFactory sessionFactory = HibernateUtils.getLocSessionFactory();
    Session session = sessionFactory.openSession();
    Transaction beginTransaction = session.beginTransaction();
    Query query = session.createQuery("from Categories c where c.status=:status AND c.catName LIKE :name");
    query.setParameter("status", 1);
    query.setParameter("name", name);
    List<Categories> catList = (List<Categories>) query.list();
    beginTransaction.commit();//from   w ww.  j  ava 2 s.  c o m
    if (catList.size() > 0)
        return catList.get(0);
    else
        return null;
}