Example usage for org.hibernate Criteria uniqueResult

List of usage examples for org.hibernate Criteria uniqueResult

Introduction

In this page you can find the example usage for org.hibernate Criteria uniqueResult.

Prototype

public Object uniqueResult() throws HibernateException;

Source Link

Document

Convenience method to return a single instance that matches the query, or null if the query returns no results.

Usage

From source file:au.org.theark.report.model.dao.ReportDao.java

License:Open Source License

public Phone getHomePhone(LinkSubjectStudy subject) {
    Phone result = null;// w w w  .jav a 2s  . com
    Criteria criteria = getSession().createCriteria(Phone.class);
    criteria.add(Restrictions.eq("person", subject.getPerson()));
    criteria.createAlias("phoneType", "pt");
    criteria.add(Restrictions.eq("pt.name", "Home"));
    criteria.setMaxResults(1);

    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("areaCode"), "areaCode");
    projectionList.add(Projections.property("phoneNumber"), "phoneNumber");
    criteria.setProjection(projectionList); // only return fields required for report
    criteria.setResultTransformer(Transformers.aliasToBean(Phone.class));

    if (criteria.uniqueResult() != null) {
        result = (Phone) criteria.uniqueResult();
    }
    return result;
}

From source file:au.org.theark.report.model.dao.ReportDao.java

License:Open Source License

public Consent getStudyCompConsent(Consent consent) {
    // Note: Should never be possible to have more than one Consent record for a
    // given a particular subject and study component
    Criteria criteria = getSession().createCriteria(Consent.class);
    if (consent != null) {
        criteria.add(Restrictions.eq("study.id", consent.getStudy().getId()));
        // must only get consents for subject in context
        criteria.add(Restrictions.eq("linkSubjectStudy.id", consent.getLinkSubjectStudy().getId()));
        // must only get consents for specific studyComp
        criteria.add(Restrictions.eq("studyComp.id", consent.getStudyComp().getId()));
        // Do NOT constrain against consentStatus or consentDate here, because we want to be able to
        // tell if they are "Not Consented" vs "Consented" with different consentStatus or consentDate.
        // if (consent.getConsentStatus() != null)
        // {//from ww  w.ja v  a2 s .c o m
        // criteria.add(Restrictions.eq("consentStatus.id", consent.getConsentStatus().getId()));
        // }
        //
        // if (consent.getConsentDate() != null)
        // {
        // criteria.add(Restrictions.eq("consentDate", consent.getConsentDate()));
        // }

    }
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.property("studyComp"), "studyComp");
    projectionList.add(Projections.property("consentStatus"), "consentStatus");
    projectionList.add(Projections.property("consentDate"), "consentDate");
    criteria.setProjection(projectionList);
    criteria.setMaxResults(1);
    criteria.setResultTransformer(Transformers.aliasToBean(Consent.class));
    Consent result = (Consent) criteria.uniqueResult();
    return result;
}

From source file:au.org.theark.report.model.dao.ReportDao.java

License:Open Source License

protected ArkFunction getArkFunctionByName(String functionName) {
    Criteria criteria = getSession().createCriteria(ArkFunction.class);
    criteria.add(Restrictions.eq("name", functionName));
    criteria.setMaxResults(1);/*www.jav a2  s  .c o  m*/
    ArkFunction arkFunction = (ArkFunction) criteria.uniqueResult();
    return arkFunction;
}

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

License:Open Source License

public boolean isStudyComponentUsed(StudyComp studyComp) {
    boolean flag = false;
    Criteria criteria = getSession().createCriteria(Consent.class);
    criteria.add(Restrictions.eq("studyComp", studyComp));
    criteria.setProjection(Projections.rowCount());
    Long i = (Long) criteria.uniqueResult();
    if (i > 0L) {
        flag = true;// w  w w.j av a  2s  .co m
    }
    return flag;
}

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

License:Open Source License

protected SubjectUidSequence getSubjectUidSequence(Study study) {
    // log.info("Getting uid seq entity for study " + study.getName());
    // Stateless sessions should be used to avoid locking the record for future update
    // by getSession(), which relies on the "open session filter" mechanism
    StatelessSession session = getStatelessSession();
    Criteria criteria = session.createCriteria(SubjectUidSequence.class);
    criteria.add(Restrictions.eq(Constants.SUBJECTUIDSEQ_STUDYNAMEID, study.getName()));
    criteria.setMaxResults(1);//from ww  w .j  a v  a 2 s  . com
    SubjectUidSequence result = (SubjectUidSequence) criteria.uniqueResult();
    session.close();
    log.warn("and got entity with lock = " + result.getInsertLock() + " for study " + study.getName());
    return result;
}

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

License:Open Source License

public LinkSubjectStudy getSubjectLinkedToStudy(Long personId, Study study)
        throws EntityNotFoundException, ArkSystemException {
    Criteria criteria = getSession().createCriteria(LinkSubjectStudy.class);
    Person person = getPerson(personId);
    criteria.add(Restrictions.eq("person", person));
    criteria.add(Restrictions.eq("study", study));
    criteria.setMaxResults(1);//from   w  w w .ja  va  2  s.c  o  m
    return (LinkSubjectStudy) criteria.uniqueResult();
}

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

License:Open Source License

public boolean isStudyCompUnique(String studyComponentName, Study study, StudyComp studyComponentToUpdate) {

    boolean isUnique = true;
    StatelessSession stateLessSession = getStatelessSession();
    Criteria criteria = stateLessSession.createCriteria(StudyComp.class);
    criteria.add(Restrictions.eq("name", studyComponentName));
    criteria.add(Restrictions.eq("study", study));
    criteria.setMaxResults(1);//from  ww w.  j  av a  2 s .  c om

    StudyComp existingComponent = (StudyComp) criteria.uniqueResult();

    if ((studyComponentToUpdate.getId() != null && studyComponentToUpdate.getId() > 0)) {

        if (existingComponent != null && !studyComponentToUpdate.getId().equals(existingComponent.getId())) {
            isUnique = false;
        }
    } else {
        if (existingComponent != null) {
            isUnique = false;
        }
    }
    stateLessSession.close();
    return isUnique;

}

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

License:Open Source License

/**
 * The count can be based on CustomFieldDisplay only instead of a left join with it using SubjectCustomFieldData
 *//*  w ww. j  ava 2  s  . c o m*/
public long getSubjectCustomFieldDataCount(LinkSubjectStudy linkSubjectStudyCriteria, ArkFunction arkFunction) {
    Criteria criteria = getSession().createCriteria(CustomFieldDisplay.class);
    criteria.createAlias("customField", "cfield", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("cfield.customFieldType", "cfieldType", JoinType.LEFT_OUTER_JOIN);
    criteria.add(Restrictions.eq("cfield.study", linkSubjectStudyCriteria.getStudy()));
    criteria.add(Restrictions.eq("cfield.arkFunction", arkFunction));
    criteria.add(Restrictions.or(Restrictions.isNull("cfield.customFieldType"),
            Restrictions.eq("cfieldType.name", "SUBJECT")));
    criteria.setProjection(Projections.rowCount());

    return (Long) criteria.uniqueResult();
}

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

License:Open Source License

/**
 * The count can be based on CustomFieldDisplay only instead of a left join with it using FamilyCustomFieldData
 *//*from  w  w  w. j  a v a2  s  . c om*/
public long getFamilyCustomFieldDataCount(LinkSubjectStudy linkSubjectStudyCriteria, ArkFunction arkFunction) {
    Criteria criteria = getSession().createCriteria(CustomFieldDisplay.class);
    criteria.createAlias("customField", "cfield", JoinType.LEFT_OUTER_JOIN);
    criteria.createAlias("cfield.customFieldType", "cfieldType", JoinType.LEFT_OUTER_JOIN);
    criteria.add(Restrictions.eq("cfield.study", linkSubjectStudyCriteria.getStudy()));
    criteria.add(Restrictions.eq("cfield.arkFunction", arkFunction));
    criteria.add(Restrictions.eq("cfieldType.name", "FAMILY"));
    criteria.setProjection(Projections.rowCount());
    return (Long) criteria.uniqueResult();
}

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

License:Open Source License

public boolean isStudyComponentHasAttachments(StudyComp studyComp) {

    boolean isFlag = false;
    Criteria criteria = getStatelessSession().createCriteria(SubjectFile.class);
    criteria.add(Restrictions.eq("studyComp", studyComp));
    criteria.setProjection(Projections.rowCount());
    Long i = (Long) criteria.uniqueResult();
    if (i > 0L) {
        isFlag = true;/*from w w  w.  j  a va  2s  .  co  m*/
    }
    return isFlag;
}