Example usage for org.hibernate Criteria createCriteria

List of usage examples for org.hibernate Criteria createCriteria

Introduction

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

Prototype

public Criteria createCriteria(String associationPath, String alias) throws HibernateException;

Source Link

Document

Create a new Criteria, "rooted" at the associated entity, assigning the given alias.

Usage

From source file:fr.utbm.repository.CourseSessionDao.java

/**
 * Filtre sur title, date et loc  /*from  w w  w .  j ava2 s .  c o  m*/
 * @param date
 * @param loc
 * @param title
 * @return 
 */
public List<Course_Session> getCourseSessionsFilter(Date date, Integer loc, String title) {
    System.out.println(date + " " + loc + " " + title);

    session = HibernateUtil.getSessionFactory().openSession();
    List<Course_Session> courseSessions = null;

    try {
        session.beginTransaction();

        Criteria criteria = session.createCriteria(Course_Session.class, "cs");
        criteria.createCriteria("courseCode", "c")
                .add(Restrictions.like("c.title", title, MatchMode.ANYWHERE).ignoreCase());
        if (date != null) {
            criteria.add(Restrictions.le("cs.startDate", date));
            criteria.add(Restrictions.ge("cs.endDate", date));
        }
        if (loc != null) {
            criteria.createAlias("locationId", "l");
            criteria.add(Restrictions.eq("l.id", loc));
        }
        courseSessions = criteria.list();
        session.getTransaction().commit();
    } catch (HibernateException he) {
        he.printStackTrace();
        if (session.getTransaction() != null) {
            try {
                session.getTransaction().rollback();
            } catch (HibernateException he2) {
                he2.printStackTrace();
            }
        }
    } finally {
        if (session != null) {
            try {
                session.close();
            } catch (HibernateException he2) {
                he2.printStackTrace();
            }
        }
    }
    for (Course_Session c : courseSessions) {
        System.out.println(c.getCourseCode());
    }
    return courseSessions;
}

From source file:gDao.util.Finder.java

License:Apache License

/**
 * Used by gDao framework. Prepare filter, sort and relation lists before searching with hibernate
 *///from   w ww .ja  v a 2 s.  com
public void prepareCriteria(Criteria criteria, Class persistentClass) {
    for (Criterion criterion : criterionList)
        criteria.add(criterion);

    for (Order order : orderList)
        criteria.addOrder(order);

    for (Filter filter : relationList) {
        String[] joinedColumns = filter.getJoinedColumns();
        for (int i = 0, joinedColumnsLength = joinedColumns.length - 1; i < joinedColumnsLength; i++) {
            String relatedColumn = joinedColumns[i];
            String firstJoin;
            String persistentClassName = StringUtils.uncapitalize(persistentClass.getSimpleName());

            if (joinedColumns[i].equals(persistentClassName))
                relatedColumn = joinedColumns[i + 1];

            if (i == 0)
                firstJoin = persistentClassName;
            else
                firstJoin = joinedColumns[i - 1];

            criteria.createCriteria(firstJoin + "." + relatedColumn, relatedColumn);
        }
    }

    if (maxResult != 0)
        criteria.setMaxResults(maxResult);

}

From source file:gov.nih.nci.eagle.service.handlers.EpidemiologicalQueryHandler.java

License:BSD License

public List getResults(QueryDTO queryDTO) {

    EPIQueryDTO epiQueryDTO = (EPIQueryDTO) queryDTO;
    Session session = sessionFactory.getCurrentSession();
    Criteria targetCrit = session.createCriteria(StudyParticipant.class);
    targetCrit.createCriteria("epidemiologicalFinding", "finding").setFetchMode("relativeCollection",
            FetchMode.JOIN);//from   w  ww . ja  va2s  . c o m
    targetCrit.createAlias("finding.tobaccoConsumptionCollection", "tc", CriteriaSpecification.LEFT_JOIN);
    targetCrit.createAlias("finding.behavioralAssessment", "ba", CriteriaSpecification.LEFT_JOIN);
    targetCrit.createAlias("finding.lifestyle", "ls", CriteriaSpecification.LEFT_JOIN);

    targetCrit.createAlias("finding.environmentalFactorCollection", "factors", CriteriaSpecification.LEFT_JOIN);

    /* 1. Handle PatientCharacteristics Criterion */
    PatientCharacteristicsCriterion patCharacterCrit = epiQueryDTO.getPatientCharacteristicsCriterion();
    if (patCharacterCrit != null)
        populatePatientCharacteristicsCriterion(patCharacterCrit, targetCrit);

    /* 2. Handle Tobacco Dependency Criterion */
    BehavioralCriterion behaviorCrit = epiQueryDTO.getBehavioralCriterion();
    if (behaviorCrit != null)
        populateBehaviorCriterion(behaviorCrit, targetCrit);

    /* Handle Tobacco Consumption Criterion */
    TobaccoConsumptionCriterion tobaccoCrit = epiQueryDTO.getTobaccoConsumptionCriterion();
    if (tobaccoCrit != null)
        populateTobaccoConsumptionCrit(tobaccoCrit, targetCrit);

    FamilyHistoryCriterion familyHistcrit = epiQueryDTO.getFamilyHistoryCriterion();
    if (familyHistcrit != null)
        populateFamilyHistoryCrit(familyHistcrit, targetCrit);

    EnvironmentalTobaccoSmokeCriterion envCrit = epiQueryDTO.getEnvironmentalTobaccoSmokeCriterion();
    if (envCrit != null && envCrit.getSmokingExposureCollection() != null) {
        Collection<SmokingExposure> exposure = envCrit.getSmokingExposureCollection();
        List<String> exposures = new ArrayList<String>();
        for (SmokingExposure ex : exposure) {
            exposures.add(ex.toString());
        }
        targetCrit.add(Restrictions.in("factors.exposureType", exposures));

    }

    // Handle patient ID criteria
    if (epiQueryDTO.getPatientIds() != null && epiQueryDTO.getPatientIds().size() > 0) {
        targetCrit.add(Restrictions.in("studySubjectIdentifier", epiQueryDTO.getPatientIds()));
    }
    targetCrit.addOrder(Order.asc("id"));
    List<StudyParticipant> l = targetCrit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

    return l;
}