Example usage for org.hibernate.criterion Restrictions between

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

Introduction

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

Prototype

public static Criterion between(String propertyName, Object low, Object high) 

Source Link

Document

Apply a "between" constraint to the named property

Usage

From source file:com.heliosapm.aa4h.parser.XMLQueryParser.java

License:Apache License

/**
 * Element Start SAX ContentHandler Method.
 * @param uri//from   w w w. j  a  v a 2 s  .  c  o  m
 * @param localName
 * @param qName
 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
 * @throws SAXException
 * TODO: Document supported tags in javadoc.
 */
@SuppressWarnings({ "unchecked" })
public void startElement(String uri, String localNameX, String qName, Attributes attrs) throws SAXException {
    if ("Query".equalsIgnoreCase(qName)) {
        queryName = attrs.getValue("name");
        rootElementName = queryName;
    } else if ("Class".equalsIgnoreCase(qName)) {
        processCriteria(attrs);
    } else if ("Union".equalsIgnoreCase(qName)) {
        inDetached = true;
        DetachedCriteria dc = DetachedCriteria.forEntityName(className);
        criteriaStack.push(dc);
    } else if ("NamedQuery".equalsIgnoreCase(qName)) {
        isNamedQuery = true;
        namedQuery = attrs.getValue("name");
        rootElementName = namedQuery;
        try {
            query = session.getNamedQuery(namedQuery);
        } catch (HibernateException he) {
            throw new SAXException("Failed to retrieve named query[" + namedQuery + "]", he);
        }
    } else if ("qparam".equalsIgnoreCase(qName)) {
        processQueryBind(attrs);
    } else if ("Join".equalsIgnoreCase(qName)) {
        processCriteria(attrs);
    } else if ("Projections".equalsIgnoreCase(qName)) {
        startProjection(attrs);
    } else if ("Projection".equalsIgnoreCase(qName)) {
        addProjection(attrs);
    } else if ("Order".equalsIgnoreCase(qName)) {
        if (isRowCountOnly() == false) {
            try {
                String name = attrs.getValue("name");
                String type = attrs.getValue("type");
                ((Criteria) criteriaStack.peek())
                        .addOrder(type.equalsIgnoreCase("asc") ? Order.asc(name) : Order.desc(name));
            } catch (Exception e) {
                throw new SAXException("Unable To Parse GreaterThan:" + attrs.getValue("name"), e);
            }
        }
    } else if ("GreaterThan".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.gt(operator.getName(), operator.getNativeValue()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse GreaterThan:" + attrs.getValue("name"), e);
        }
    } else if ("GreaterThanOrEqual".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.ge(operator.getName(), operator.getNativeValue()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse GreaterThanOrEqual:" + attrs.getValue("name"), e);
        }
    } else if ("LessThan".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.lt(operator.getName(), operator.getNativeValue()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse LessThan:" + attrs.getValue("name"), e);
        }
    } else if ("LessThanOrEqual".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.le(operator.getName(), operator.getNativeValue()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse LessThanOrEqual:" + attrs.getValue("name"), e);
        }
    } else if ("Equals".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            if ("true".equalsIgnoreCase(attrs.getValue("not"))) {
                addCriterion(Restrictions.not(Restrictions.eq(operator.getName(), operator.getNativeValue())));
            } else {
                addCriterion(Restrictions.eq(operator.getName(), operator.getNativeValue()));
            }

        } catch (Exception e) {
            throw new SAXException("Unable To Parse Equals:" + attrs.getValue("name"), e);
        }
    } else if ("Alias".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            ((Criteria) criteriaStack.peek()).createAlias(operator.getName(), operator.getValue());
        } catch (Exception e) {
            throw new SAXException("Unable To Create Alias:" + attrs.getValue("name"), e);
        }
    } else if ("GreaterThanProperty".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.gtProperty(operator.getName(), operator.getName2()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse GreaterThanProperty:" + attrs.getValue("name"), e);
        }
    } else if ("GreaterThanOrEqualProperty".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.geProperty(operator.getName(), operator.getName2()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse GreaterThanOrEqualProperty:" + attrs.getValue("name"), e);
        }
    } else if ("LessThanProperty".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.ltProperty(operator.getName(), operator.getName2()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse LessThanProperty:" + attrs.getValue("name"), e);
        }
    } else if ("LessThanOrEqualProperty".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.leProperty(operator.getName(), operator.getName2()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse LessThanOrEqualProperty:" + attrs.getValue("name"), e);
        }
    } else if ("EqualsProperty".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            if ("true".equalsIgnoreCase(attrs.getValue("not"))) {
                addCriterion(
                        Restrictions.not(Restrictions.eqProperty(operator.getName(), operator.getName2())));
            } else {
                addCriterion(Restrictions.eqProperty(operator.getName(), operator.getName2()));
            }
        } catch (Exception e) {
            throw new SAXException("Unable To Parse EqualsProperty:" + attrs.getValue("name"), e);
        }
    } else if ("Like".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.like(operator.getName(), operator.getNativeValue()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse Like:" + attrs.getValue("name"), e);
        }
    } else if ("Between".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.between(operator.getName(), operator.getNativeValue(),
                    operator.getNativeValue2()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse Between:" + attrs.getValue("name"), e);
        }
    } else if ("IsEmpty".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.isEmpty(operator.getName()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse IsEmpty:" + attrs.getValue("name"), e);
        }
    } else if ("IsNotEmpty".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.isNotEmpty(operator.getName()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse IsNotEmpty:" + attrs.getValue("name"), e);
        }
    } else if ("IsNull".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.isNull(operator.getName()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse IsNull:" + attrs.getValue("name"), e);
        }
    } else if ("IsNotNull".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            addCriterion(Restrictions.isNotNull(operator.getName()));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse IsNotNull:" + attrs.getValue("name"), e);
        }
    } else if ("In".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            if (operator.isLiteral()) {
                addCriterion(new LiteralInExpression(operator.getName(), (String) operator.getNativeValue()));
            } else {
                addCriterion(Restrictions.in(operator.getName(), (Collection) operator.getNativeValue()));
            }
        } catch (Exception e) {
            throw new SAXException("Unable To Parse In:" + attrs.getValue("name"), e);
        }
    } else if ("SizeEquals".equalsIgnoreCase(qName)) {
        try {
            Operator operator = new Operator(attrs);
            int i = ((Integer) operator.getNativeValue()).intValue();
            addCriterion(Restrictions.sizeEq(operator.getName(), i));
        } catch (Exception e) {
            throw new SAXException("Unable To Parse SizeEquals:" + attrs.getValue("name"), e);
        }
    } else if ("Not".equalsIgnoreCase(qName)) {
        notStack.push(new Object());
    } else if ("Or".equalsIgnoreCase(qName)) {
        opStack.push(Restrictions.disjunction());
    } else if ("And".equalsIgnoreCase(qName)) {
        opStack.push(Restrictions.conjunction());
    } else {
        throw new SAXException("Element Name[" + qName + "] Not Recognized.");
    }
}

From source file:com.hm.modelo.DAOUsu.java

public String obtenerPorNOmbre(String nombre) throws Exception {
    SessionFactory factory = HIbernate.getSessionFactory();
    Session sesion = factory.openSession();
    Transaction tranza = sesion.beginTransaction();

    Criteria cri = sesion.createCriteria(Usu.class).add(Restrictions.like("nombre", nombre + "%"));
    Criteria cri2 = sesion.createCriteria(Usu.class).add(Restrictions.eq("nombre", nombre));
    Criteria cri3 = sesion.createCriteria(Usu.class).add(Restrictions.between("edad", 18, 40))
            .addOrder(Order.asc("nombre"));
    Criteria cri4 = sesion.createCriteria(Usu.class).add(Restrictions.lt("sueldo", new Integer(4000)));
    Criteria cri5 = sesion.createCriteria(Usu.class).add(Restrictions.gt("sueldo", new Integer(4000)));

    ArrayList<Usu> usuarios = (ArrayList<Usu>) cri.list();

    ObjectMapper mapper = new ObjectMapper();

    Map<String, ArrayList<Usu>> singletonMap = Collections.singletonMap("usuarios", usuarios);

    tranza.commit();//from  w  w w.  java2 s.c  om
    sesion.close();

    return mapper.writeValueAsString(singletonMap);
}

From source file:com.hmsinc.epicenter.model.surveillance.impl.SurveillanceRepositoryImpl.java

License:Open Source License

/**
 * @param c/*from   w  w w  . j  a  v a  2 s  .c  o m*/
 * @param startDate
 * @param endDate
 * @param includeAll
 * @param includeFacilityLocation
 * @param filter
 * @return
 */
private Criteria applyAnomalyCriteria(final Criteria c, final DateTime startDate, final DateTime endDate,
        final boolean includeAll, final Geometry filter, final Geometry excludeFacilityEventsFilter) {

    if (startDate == null && endDate != null) {
        c.add(Restrictions.lt("analysisTimestamp", endDate));
    } else if (startDate != null && endDate == null) {
        c.add(Restrictions.gt("analysisTimestamp", startDate));
    } else if (startDate != null && endDate != null) {
        c.add(Restrictions.between("analysisTimestamp", startDate, endDate));
    }

    if (!includeAll) {
        c.add(Restrictions.isEmpty("investigations")).createCriteria("disposition")
                .add(Restrictions.eq("type", WorkflowStateType.INITIAL));
    }

    if (filter != null && excludeFacilityEventsFilter == null) {

        c.createCriteria("geography").add(SpatialRestrictions.withinOrFilter("geometry", filter, 1000, true));

    } else if (filter == null && excludeFacilityEventsFilter != null) {

        final Conjunction conjunction = Restrictions.conjunction();
        c.createAlias("geography", "eventGeography");
        c.createAlias("task", "eventTask");

        conjunction.add(Restrictions.eq("eventTask.location", AnalysisLocation.FACILITY));
        conjunction.add(SpatialRestrictions.withinOrFilter("eventGeography.geometry",
                excludeFacilityEventsFilter, 1000, true));

        c.add(Restrictions.not(conjunction));

    } else if (filter != null && excludeFacilityEventsFilter != null) {

        final Conjunction conjunction = Restrictions.conjunction();
        c.createAlias("geography", "eventGeography");
        c.createAlias("task", "eventTask");

        // Find events where we have unlimited access
        conjunction.add(SpatialRestrictions.withinOrFilter("eventGeography.geometry", filter, 1000, true));

        // Filter events in the limited region
        final Conjunction facilityEventsConjunction = Restrictions.conjunction();
        facilityEventsConjunction.add(Restrictions.eq("eventTask.location", AnalysisLocation.FACILITY));
        facilityEventsConjunction.add(SpatialRestrictions.withinOrFilter("eventGeography.geometry",
                excludeFacilityEventsFilter, 1000, true));

        conjunction.add(Restrictions.not(facilityEventsConjunction));

        c.add(conjunction);
    }

    return c;
}

From source file:com.hmsinc.epicenter.model.workflow.impl.WorkflowRepositoryImpl.java

License:Open Source License

/**
 * Basic filter for investigations./*ww w  . j av a2 s  . com*/
 * 
 * @param c
 * @param startDate
 * @param endDate
 * @param geometry
 * @param organizations
 * @param showAll
 * @return
 */
private Criteria applyInvestigationCriteria(final Criteria c, DateTime startDate, DateTime endDate,
        Geometry geometry, Collection<Organization> organizations, boolean showAll) {

    if (startDate != null) {
        if (endDate == null) {
            c.add(Restrictions.gt("timestamp", startDate));
        } else {
            c.add(Restrictions.between("timestamp", startDate, endDate));
        }
    }

    if (organizations != null && organizations.size() > 0) {
        c.add(Restrictions.in("organization", organizations));
    }

    if (geometry != null) {
        c.add(Subqueries.exists(getInvestigationGeometryFilter(geometry)));
    }

    if (showAll == false) {
        c.createCriteria("state").add(Restrictions.ne("stateType", WorkflowStateType.TERMINAL));
    }

    return c;
}

From source file:com.hp.dao.ForLeaveDAOImpl.java

@Override
public List<ForLeave> getForLeaveList(Staff staff, Date date) {
    Session session = HibernateSessionFactory.getSessionFactory().openSession();
    Transaction transaction = session.beginTransaction();
    try {/*from  w w w.  j a  va 2 s.  c o m*/
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        c.add(Calendar.DAY_OF_MONTH, 1);

        Criteria criteria = session.createCriteria(ForLeave.class);
        criteria.add(Restrictions.eq("staff", staff));
        criteria.add(Restrictions.between("timeAt", date, c.getTime()));
        criteria.addOrder(Order.asc("timeAt"));
        return criteria.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    finally {
        session.close();
    }

    return null;
}

From source file:com.hp.dao.SetLunchDAOImpl.java

@Override
public List<SetLunch> getSetLunchList(Staff staff, Date date) {
    Session session = HibernateSessionFactory.getSessionFactory().openSession();
    Transaction transaction = session.beginTransaction();
    try {/*from   w w w  .  j  av a  2s.co m*/
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        c.add(Calendar.DAY_OF_MONTH, 1);

        Criteria criteria = session.createCriteria(SetLunch.class);
        criteria.add(Restrictions.eq("staff", staff));
        criteria.add(Restrictions.between("timeAt", date, c.getTime()));
        criteria.addOrder(Order.asc("timeAt"));
        return criteria.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    finally {
        session.close();
    }

    return null;
}

From source file:com.hp.dao.TimeKeeperDAOImpl.java

@Override
public List<TimeKeeper> getTimeKeeperList(Staff staff, Date date) {
    Session session = HibernateSessionFactory.getSessionFactory().openSession();
    Transaction transaction = session.beginTransaction();
    try {//from   ww w. j  a  v a2 s.c o m
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        c.add(Calendar.DAY_OF_MONTH, 1);

        Criteria criteria = session.createCriteria(TimeKeeper.class);
        criteria.add(Restrictions.eq("staff", staff));
        criteria.add(Restrictions.between("timeAt", date, c.getTime()));
        criteria.addOrder(Order.asc("timeAt"));
        return criteria.list();
    } catch (Exception e) {
        e.printStackTrace();
    }

    finally {
        session.close();
    }

    return null;
}

From source file:com.ihsolution.hqipo.dao.utils.QueryHelper.java

License:Open Source License

/**
 * Main conversion method/*  www.  j  a va 2 s  .  c om*/
 * @param fieldName
 * @param fieldVal
 * @param oper (=, equal, IN ...)
 * @param j (AND OR)
 * @return
 */
public QueryHelper addFieldAndVal(String fieldName, Object fieldVal, String oper, Junction j) {

    boolean isValString = fieldVal instanceof String;
    String str = "";
    if (oper == null || "".equals(oper)) {
        oper = "equal";
    }

    if (isValString)
        str = ((String) fieldVal).trim();
    if ("equal".equals(oper)) {
        if (isValString) {
            j.add(Restrictions.eq(fieldName, str).ignoreCase());
        } else
            j.add(Restrictions.eq(fieldName, fieldVal));
    } else if ("notEqual".equals(oper)) {
        if (isValString) {
            j.add(Restrictions.ne(fieldName, str).ignoreCase());
        } else
            j.add(Restrictions.ne(fieldName, fieldVal));
    } else if ("null".equals(oper)) {
        j.add(Restrictions.isNull(fieldName));
    } else if ("notNull".equals(oper)) {
        j.add(Restrictions.isNotNull(fieldName));
    } else if ("notExists".equals(oper)) {
        j.add(Restrictions.sqlRestriction(fieldVal.toString()));
    } else if ("Exists".equals(oper)) {
        j.add(Restrictions.sqlRestriction(fieldVal.toString()));
    } else if (isValString) {
        MatchMode mm = getMatchMode(oper);
        if (mm != null)
            j.add(Restrictions.ilike(fieldName, str, mm));
    } else if ("le".equals(oper))
        j.add(Restrictions.le(fieldName, fieldVal));

    else if ("ge".equals(oper))
        j.add(Restrictions.ge(fieldName, fieldVal));
    else if ("gtProperty".equals(oper)) {
        String[] spl = ((String) fieldVal).split(";");
        if (spl.length == 2)
            j.add(Restrictions.gtProperty(spl[0], spl[1]));
        else
            j.add(Restrictions.gt(fieldName, fieldVal));
    } else if ("in".equals(oper)) {
        if (fieldVal instanceof Collection)
            j.add(Restrictions.in(fieldName, (Collection) fieldVal));
        else if (fieldVal instanceof Object[])
            j.add(Restrictions.in(fieldName, (Object[]) fieldVal));
        else
            throw new IllegalArgumentException(
                    "QueryHelper.IN illegal argument type. Should be Collection or Object[]");
    } else if ("notIn".equals(oper)) {
        if (fieldVal instanceof Collection)
            j.add(Restrictions.not(Restrictions.in(fieldName, (Collection) fieldVal)));
        else if (fieldVal instanceof Object[])
            j.add(Restrictions.not(Restrictions.in(fieldName, (Object[]) fieldVal)));
        else
            throw new IllegalArgumentException(
                    "QueryHelper.NOTIN illegal argument type. Should be Collection or Object[]");

    } else if ("between".equals(oper)) {
        Collection objs = (Collection) fieldVal;
        Iterator it2 = objs.iterator();
        Object obj1 = it2.next();
        Object obj2 = it2.next();

        j.add(Restrictions.between(fieldName, obj1 instanceof String ? obj1.toString().toLowerCase() : obj1,
                obj2 instanceof String ? obj2.toString().toLowerCase() : obj2));
    } else
        j.add(Restrictions.eq(fieldName, fieldVal));

    return this;
}

From source file:com.inkubator.hrm.dao.impl.WtHolidayDaoImpl.java

@Override
public List<WtHoliday> getBetweenDate(Date start, Date end) {
    Criteria criteria = getCurrentSession().createCriteria(getEntityClass());
    criteria.add(Restrictions.between("holidayDate", start, end));
    return criteria.list();

}

From source file:com.inkubator.hrm.dao.impl.WtHolidayDaoImpl.java

@Override
public Long getTotalBetweenDate(Date start, Date end) {
    Criteria criteria = getCurrentSession().createCriteria(getEntityClass());
    criteria.add(Restrictions.between("holidayDate", start, end));
    return (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();
}