Example usage for org.hibernate.criterion Restrictions sqlRestriction

List of usage examples for org.hibernate.criterion Restrictions sqlRestriction

Introduction

In this page you can find the example usage for org.hibernate.criterion Restrictions sqlRestriction.

Prototype

public static Criterion sqlRestriction(String sql) 

Source Link

Document

Apply a constraint expressed in SQL with no JDBC parameters.

Usage

From source file:uk.org.openeyes.DatabaseFunctions.java

/**
 *
 * @param hosNum//from w  ww  .ja  v  a 2  s  .  c o  m
 * @param gender
 * @param birthDate
 */
public void searchPatient(String hosNum, char gender, Calendar birthDate) {
    Session session = sessionFactory.openSession();
    Criteria crit = session.createCriteria(Patient.class);

    // add leading 0s to the hosNum string
    if (hosNum.length() < 7) {
        hosNum = ("0000000" + hosNum).substring(hosNum.length());
    }

    //crit.add(Restrictions.eq("hosNum",hosNum));
    crit.add(Restrictions.sqlRestriction("lower(hos_num) = '" + hosNum.toLowerCase() + "'"));
    // we should search for M or F only
    if (Character.toString(gender).equals("F") || Character.toString(gender).equals("M")) {
        crit.add(Restrictions.eq("gender", Character.toString(gender)));
    }
    int dateYear = birthDate.get(Calendar.YEAR);
    int dateMonth = birthDate.get(Calendar.MONTH) + 1;
    int dateDay = birthDate.get(Calendar.DAY_OF_MONTH);
    crit.add(Restrictions.sqlRestriction("dob = '" + dateYear + "-" + dateMonth + "-" + dateDay + "'"));
    List patientList = crit.list();

    if (patientList.isEmpty()) {
        // TODO: How to handle this case??
        dicomLogger.addToRawOutput("ERROR: Patient not found for the data specified (hos_num: " + hosNum
                + ", gender: " + gender + ", dob: " + dateYear + "-" + dateMonth + "-" + dateDay + ")");
    } else if (patientList.size() > 1) {
        // TODO: How to handle this case??
        dicomLogger.addToRawOutput("ERROR: More than 1 record found for patient (hos_num: " + hosNum
                + ", gender: " + gender + ", dob: " + dateYear + "-" + dateMonth + "-" + dateDay + ")");
    } else {
        // TODO: is everything OK?
        selectedPatient = (Patient) patientList.get(0);
    }
    if (selectedPatient != null) {
        dicomLogger.addToRawOutput("Selected patient: " + selectedPatient);
    }
    session.close();
}