Example usage for org.hibernate.criterion Restrictions le

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

Introduction

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

Prototype

public static SimpleExpression le(String propertyName, Object value) 

Source Link

Document

Apply a "less than or equal" constraint to the named property

Usage

From source file:cz.jirutka.rsql.hibernate.AbstractCriterionBuilderTest.java

License:Open Source License

@Test
public void testCreateCriterion3args() {
    String property = "foo";
    Criterion exptected;/*from  ww w. j a va2s. co  m*/
    Criterion actual;

    exptected = Restrictions.eq(property, "bar");
    actual = instance.createCriterion(property, Comparison.EQUAL, "bar");
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.ilike(property, "bar%");
    actual = instance.createCriterion(property, Comparison.EQUAL, "bar*");
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.ne(property, "bar");
    actual = instance.createCriterion(property, Comparison.NOT_EQUAL, "bar");
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.not(Restrictions.ilike(property, "%bar"));
    actual = instance.createCriterion(property, Comparison.NOT_EQUAL, "*bar");
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.gt(property, 42);
    actual = instance.createCriterion(property, Comparison.GREATER_THAN, 42);
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.ge(property, -42);
    actual = instance.createCriterion(property, Comparison.GREATER_EQUAL, -42);
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.lt(property, 42.2);
    actual = instance.createCriterion(property, Comparison.LESS_THAN, 42.2);
    assertEquals(exptected.toString(), actual.toString());

    exptected = Restrictions.le(property, -42.2);
    actual = instance.createCriterion(property, Comparison.LESS_EQUAL, -42.2);
    assertEquals(exptected.toString(), actual.toString());

}

From source file:cz.jirutka.rsql.visitor.hibernate.HibernateCriterionVisitor.java

License:Apache License

@Override
public Criterion visit(ComparisonNode node) {
    String name = node.getSelector();
    /*/*w  w w. ja v  a 2  s  .  co  m*/
     Get the path and property names
     */
    Tuple<Type, String>[] path = getPath(root, name);
    Type type = getType(path);
    boolean isPrimitive = type instanceof StringRepresentableType;
    boolean isCustom = type instanceof CustomType;
    boolean isCollection = type instanceof CollectionType;

    String exp = getExpression(path, isCollection);
    List<Object> arguments = new ArrayList<Object>(node.getArguments().size());
    for (String argument : node.getArguments()) {
        Object value = argument;
        if (isPrimitive) {
            value = ((StringRepresentableType) type).fromStringValue((String) value);
        } else if (isCustom) {
            value = ((CustomType) type).stringToObject((String) value);
        } else if (isCollection) {
            value = IntegerType.INSTANCE.fromString((String) value);
        }
        if (value instanceof String) {
            value = toSqlWildcardString((String) value);
        }
        arguments.add(value);
    }
    ComparisonOperator operator = node.getOperator();

    assert arguments.size() >= 1;

    Object firstArgument = arguments.get(0);
    if (operator.equals(RSQLOperators.EQUAL)) {
        if (!isCollection) {
            if (String.class.isInstance(firstArgument) && ((String) firstArgument).contains("%")) {
                return Restrictions.ilike(exp, firstArgument);
            } else {
                return Restrictions.eq(exp, firstArgument);
            }
        } else {
            return Restrictions.sizeEq(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.NOT_EQUAL)) {
        if (!isCollection) {
            if (String.class.isInstance(firstArgument) && ((String) firstArgument).contains("%")) {
                return Restrictions.not(Restrictions.ilike(exp, firstArgument));
            } else {
                return Restrictions.ne(exp, firstArgument);
            }
        } else {
            return Restrictions.sizeNe(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.GREATER_THAN)) {
        if (!isCollection) {
            return Restrictions.gt(exp, firstArgument);
        } else {
            return Restrictions.sizeGt(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.GREATER_THAN_OR_EQUAL)) {
        if (!isCollection) {
            return Restrictions.ge(exp, firstArgument);
        } else {
            return Restrictions.sizeGe(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.LESS_THAN)) {
        if (!isCollection) {
            return Restrictions.lt(exp, firstArgument);
        } else {
            return Restrictions.sizeLt(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.LESS_THAN_OR_EQUAL)) {
        if (!isCollection) {
            return Restrictions.le(exp, firstArgument);
        } else {
            return Restrictions.sizeLe(exp, (Integer) firstArgument);
        }
    } else if (operator.equals(RSQLOperators.IN)) {
        if (!isCollection) {
            return Restrictions.in(exp, arguments);
        }
    } else if (operator.equals(RSQLOperators.NOT_IN)) {
        if (!isCollection) {
            return Restrictions.not(Restrictions.in(exp, arguments));
        }
    }
    throw new IllegalArgumentException("Unknown operation " + operator.toString() + " for property" + name);
}

From source file:cz.zcu.kiv.eegdatabase.data.dao.SimpleReservationDao.java

License:Apache License

public int createChecked(Reservation newInstance) throws DaoException {

    if (newInstance.getStartTime().getTime() >= newInstance.getEndTime().getTime()) {
        throw new DaoException("Time of start cannot be lower than or equal to time of end!");
    }/*  w w w .  j  ava2s.com*/
    Criteria overlapCriteria = getSession().createCriteria(Reservation.class);
    //start overlap
    LogicalExpression startOverlap = Restrictions.and(
            Restrictions.and(Restrictions.ge("startTime", newInstance.getStartTime()),
                    Restrictions.ge("endTime", newInstance.getEndTime())),
            Restrictions.le("startTime", newInstance.getEndTime()));
    //end overlap
    LogicalExpression endOverlap = Restrictions.and(
            Restrictions.and(Restrictions.le("startTime", newInstance.getStartTime()),
                    Restrictions.le("endTime", newInstance.getEndTime())),
            Restrictions.ge("endTime", newInstance.getStartTime()));
    //include overlap
    LogicalExpression inOverlap = Restrictions.and(Restrictions.le("startTime", newInstance.getStartTime()),
            Restrictions.ge("endTime", newInstance.getEndTime()));

    //complete overlap
    LogicalExpression completeOverlap = Restrictions.and(
            Restrictions.ge("startTime", newInstance.getStartTime()),
            Restrictions.le("endTime", newInstance.getEndTime()));

    overlapCriteria.add(Restrictions.or(Restrictions.or(inOverlap, completeOverlap),
            Restrictions.or(startOverlap, endOverlap)));

    //if some overlap was found, it is an error
    if (overlapCriteria.list().size() > 0) {
        throw new DaoException(
                "Reservation could not be created due to existing records within the time range.");
    }

    return super.create(newInstance);
}

From source file:dao.CondominioDAO.java

public static List<Condominio> findByFiltros(Integer idMorador, Date dtInicial, Date dtFinal, boolean emAberto)
        throws Exception {
    SessionFactory sf = HibernateUtil.getSessionFactory();
    Session session = sf.openSession();/*from w w  w  .ja  va  2  s  . co m*/
    Criteria crit = session.createCriteria(Condominio.class);
    Criteria subCrit = crit.createCriteria("pagamento", "pagamento", JoinType.INNER_JOIN);
    Criteria subCrit2 = crit.createCriteria("morador", "morador", JoinType.INNER_JOIN);

    if (emAberto) {
        subCrit.add(Restrictions.isNull("data"));
        crit.add(Restrictions.lt("data", new Date()));
    }
    if (idMorador != null && idMorador > 0) {
        subCrit2.add(Restrictions.eq("id", idMorador));
    }
    if (dtInicial != null) {
        crit.add(Restrictions.ge("data", dtInicial));
    }
    if (dtFinal != null) {
        crit.add(Restrictions.le("data", dtFinal));
    }

    List<Condominio> list = crit.list();
    session.flush();
    session.close();
    return list;
}

From source file:Dao.ShowingDAO.java

public List<Showing> getShowingByMovieAndTheatreAndTime(Movie movie, Theatre theatre, Timestamp date) {
    Session session = this.sessionFactory.getCurrentSession();

    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//from  w  ww.ja v  a2s  .com
    cal.add(Calendar.DAY_OF_WEEK, 1);
    Timestamp tomorrow = new Timestamp(cal.getTime().getTime());

    List showings = session.createCriteria(Showing.class).add(Restrictions.eq("movie", movie))
            .add(Restrictions.eq("theatre", theatre)).add(Restrictions.ge("time", date))
            .add(Restrictions.le("time", tomorrow)).list();
    if (showings.isEmpty()) {
        return null;
    } else {
        return showings;
    }
}

From source file:data.dao.CarDao.java

public List<Car> getCarLesserPrice(BigDecimal price) {

    Criteria cr = currentSession().createCriteria(getSupportedClass());
    cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    cr.add(Restrictions.le("cmPrice", price.doubleValue()));
    cr.add(Restrictions.gt("cmPrice", Double.valueOf("0.0")));
    return cr.list();
}

From source file:database.News.java

License:Open Source License

/**
 * Gets the news companies good rep le polarity.
 *
 * @param polarity the polarity/*www.j  av  a  2 s.com*/
 * @return the news companies good rep le polarity
 */
public static List<News> getNewsCompaniesGoodRepLePolarity(double polarity) {

    Criterion polarity_cr = Restrictions.le("detail.total_polarity", polarity);
    Criterion disj_cr = getNewsOfCompaniesWithGoodReputation();

    return getResultsWithTwoCriterions(polarity_cr, disj_cr);

}

From source file:db.helpers.FestivalHelper.java

public List<Festival> getFestivals(String name, String location, String artist, Date dateFrom, Date dateTo) {
    List<Festival> festivals = new ArrayList<>();
    session = HibernateUtil.getSessionFactory().getCurrentSession();
    org.hibernate.Transaction tx = session.beginTransaction();
    try {/*from w w  w  .j  a va  2s.c  om*/
        Criteria c = session.createCriteria(Festival.class);
        if (name != null && !("".equals(name))) {
            c.add(Restrictions.ilike("name", name, MatchMode.ANYWHERE));
        }
        if (location != null && !("".equals(location))) {
            c.add(Restrictions.ilike("location", location, MatchMode.ANYWHERE));
        }
        if (dateFrom != null) {
            c.add(Restrictions.ge("dateStart", dateFrom));
        }
        if (dateTo != null) {
            c.add(Restrictions.le("dateEnd", dateTo));
        }
        if (artist != null && !("".equals(artist))) {
            c.createAlias("performances", "performance");
            c.add(Restrictions.ilike("performance.artist", artist, MatchMode.ANYWHERE));
        }
        festivals = c.list();
        tx.commit();
    } catch (HibernateException ex) {
        tx.rollback();
    }
    return festivals;
}

From source file:db.helpers.FestivalHelper.java

public List<Festival> getDaysEvents(Date date) {
    List<Festival> festivals = new ArrayList<>();
    session = HibernateUtil.getSessionFactory().getCurrentSession();
    org.hibernate.Transaction tx = session.beginTransaction();
    try {/*w w w.j a v a2  s .co  m*/
        Criteria c = session.createCriteria(Festival.class);
        c.add(Restrictions.le("dateStart", date));
        c.add(Restrictions.ge("dateEnd", date));
        festivals = c.list();
        tx.commit();
    } catch (HibernateException ex) {
        tx.rollback();
    }
    return festivals;
}

From source file:de.escidoc.core.common.business.filter.CqlFilter.java

License:Open Source License

/**
 * Evaluate a CQL relation.//  www  .  jav a2 s. c o  m
 * 
 * @param relation
 *            CQL relation
 * @param propertyName
 *            left side of the statement
 * @param value
 *            right side of the statement
 * @param useLike
 *            use LIKE instead of = in case of an equality relation
 * @return Hibernate query reflecting the given CQL query
 * @throws InvalidSearchQueryException
 *             thrown if the given search query could not be translated into a SQL query
 */
protected Criterion evaluate(final CQLRelation relation, final String propertyName, final Object value,
        final boolean useLike) throws InvalidSearchQueryException {
    final Criterion result;
    final String rel = relation.getBase();

    if (value == null || value.toString().length() == 0) {
        result = Restrictions.isNull(propertyName);
    } else {
        if ("<".equals(rel)) {
            result = Restrictions.lt(propertyName, value);
        } else if ("<=".equals(rel)) {
            result = Restrictions.le(propertyName, value);
        } else if ("=".equals(rel)) {
            result = useLike ? Restrictions.like(propertyName, value) : Restrictions.eq(propertyName, value);
        } else if (">=".equals(rel)) {
            result = Restrictions.ge(propertyName, value);
        } else if (">".equals(rel)) {
            result = Restrictions.gt(propertyName, value);
        } else if ("<>".equals(rel)) {
            result = Restrictions.ne(propertyName, value);
        } else {
            throw new InvalidSearchQueryException(rel + ": relation not implemented");
        }
    }
    return result;
}