Example usage for org.hibernate.criterion Restrictions lt

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

Introduction

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

Prototype

public static SimpleExpression lt(String propertyName, Object value) 

Source Link

Document

Apply a "less than" constraint to the named property

Usage

From source file:services.Service.java

public List<T> findBy(String column, Serializable value, String operador) {
    Session s = getSession();// w  w  w  . jav a2  s . c  o  m
    synchronized (s) {
        try {

            Criteria c = s.createCriteria(classRef);

            for (int i = 0; i < operador.length(); i++) {
                char operador1 = operador.charAt(i);

                switch (operador1) {
                case '=':
                    c.add(Restrictions.eq(column, value));
                    break;
                case '>':
                    c.add(Restrictions.gt(column, value));
                    break;
                case '<':
                    c.add(Restrictions.lt(column, value));
                    break;
                }
            }
            return c.list();
            //                return s.createQuery("from " + classRef.getSimpleName() + " where " + column + " = ").list();
        } finally {
            autoClose(s);
        }
    }
    //        return getSession();
}

From source file:to.etc.domui.hibernate.model.CriteriaCreatingVisitor.java

License:Open Source License

@Override
public void visitPropertyComparison(QPropertyComparison n) throws Exception {
    QOperatorNode rhs = n.getExpr();//from   w ww . j a  va2  s  . c om
    String name = n.getProperty();
    QLiteral lit = null;
    if (rhs.getOperation() == QOperation.LITERAL) {
        lit = (QLiteral) rhs;
    } else if (rhs.getOperation() == QOperation.SELECTION_SUBQUERY) {
        handlePropertySubcriteriaComparison(n);
        return;
    } else
        throw new IllegalStateException(
                "Unknown operands to " + n.getOperation() + ": " + name + " and " + rhs.getOperation());

    //-- If prop refers to some relation (dotted pair):
    name = parseSubcriteria(name);

    Criterion last = null;
    switch (n.getOperation()) {
    default:
        throw new IllegalStateException("Unexpected operation: " + n.getOperation());

    case EQ:
        if (lit.getValue() == null) {
            last = Restrictions.isNull(name);
            break;
        }
        last = Restrictions.eq(name, lit.getValue());
        break;
    case NE:
        if (lit.getValue() == null) {
            last = Restrictions.isNotNull(name);
            break;
        }
        last = Restrictions.ne(name, lit.getValue());
        break;
    case GT:
        last = Restrictions.gt(name, lit.getValue());
        break;
    case GE:
        last = Restrictions.ge(name, lit.getValue());
        break;
    case LT:
        last = Restrictions.lt(name, lit.getValue());
        break;
    case LE:
        last = Restrictions.le(name, lit.getValue());
        break;
    case LIKE:
        handleLikeOperation(name, m_targetProperty, lit.getValue());
        return;
    case ILIKE:
        last = Restrictions.ilike(name, lit.getValue());
        break;
    }

    m_last = last;
}

From source file:ubc.pavlab.aspiredb.server.dao.CriteriaBuilder.java

License:Apache License

/**
 * @param operator/*from   w w w . ja v a  2 s .  c  o  m*/
 * @param property
 * @param value
 * @return
 */
private static Criterion createNumericalCriterion(Operator operator, String property, NumericValue value) {
    Criterion criterion;
    switch (operator) {
    case NUMERIC_GREATER:
        criterion = Restrictions.gt(property, value.getValue());
        break;
    case NUMERIC_LESS:
        criterion = Restrictions.lt(property, value.getValue());
        break;
    case NUMERIC_EQUAL:
        criterion = Restrictions.eq(property, value.getValue());
        break;
    case NUMERIC_NOT_EQUAL:
        criterion = Restrictions.ne(property, value.getValue());
        break;
    default:
        throw new IllegalArgumentException();
    }
    return criterion;
}

From source file:unique.Consultas.ConsultaNiveis.java

private void btnConsultarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnConsultarActionPerformed
    // TODO add your handling code here:
    int x = tblNiveis.getRowCount();
    DefaultTableModel model = (DefaultTableModel) tblNiveis.getModel();
    for (int i = 0; i < x; i++) {
        model.removeRow(0);/*from   w w  w . j  ava2  s .c o m*/
    }

    try {
        conexao = HibernateUtil.openSession();
        Criteria crit = conexao.createCriteria(Nivel.class);
        if (!txtCodigo.getText().isEmpty())
            crit.add(Restrictions.eq("Codigo", txtCodigo.getText()));

        if (!txtNome.getText().isEmpty())
            crit.add(Restrictions.eq("Nome", txtNome.getText()));

        String temp = txtValorAulas.getText();
        temp = temp.replaceAll("\\.", "");
        temp = temp.replaceAll("\\,", ".");
        Double valor = Double.parseDouble(temp);

        if (valor > 0) {
            switch (comboAulas.getSelectedIndex()) {
            case 0:
                crit.add(Restrictions.lt("ValorAulas", valor)); //less than
            case 1:
                crit.add(Restrictions.eq("ValorAulas", valor)); //equals
            case 2:
                crit.add(Restrictions.gt("ValorAulas", valor)); //greater than
            }
        }

        temp = txtValorMaterial.getText();
        temp = temp.replaceAll("\\.", "");
        temp = temp.replaceAll("\\,", ".");
        valor = Double.parseDouble(temp);

        if (valor > 0) {
            switch (comboMaterial.getSelectedIndex()) {
            case 0:
                crit.add(Restrictions.lt("ValorMaterial", valor)); //less than
            case 1:
                crit.add(Restrictions.eq("ValorMaterial", valor)); //equals
            case 2:
                crit.add(Restrictions.gt("ValorMaterial", valor)); //greater than
            }
        }

        crit.addOrder(Order.asc("Codigo"));
        List<Nivel> results = crit.list();
        String codigo, nome, descricao, valorAulas, valorMaterial, duracao;

        for (Nivel n : results) {
            if (n.getCodigo() == null)
                codigo = " ";
            else
                codigo = n.getCodigo();

            if (n.getNome() == null)
                nome = " ";
            else
                nome = n.getNome();

            if (n.getDescricao() == null)
                descricao = " ";
            else
                descricao = n.getDescricao();

            if (n.getValorAulas() == null)
                valorAulas = "0.000,00";
            else
                valorAulas = n.getValorAulas().toString();

            if (n.getValorMaterial() == null)
                valorMaterial = "0.000,00";
            else
                valorMaterial = n.getValorMaterial().toString();

            if (n.getDuracao() == null)
                duracao = "0";
            else
                duracao = n.getDuracao().toString();

            String vip = "No";

            if (n.isVIP())
                vip = "Sim";

            String[] linha = new String[] { n.getID().toString(), codigo, nome, descricao, valorAulas,
                    valorMaterial, duracao, vip };

            model.addRow(linha);
        }

        conexao.close();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Operao mal sucedida. Motivo: " + e.getMessage(), "Erro",
                JOptionPane.ERROR_MESSAGE);
    }

    resizeColumnWidth(tblNiveis);
}

From source file:vn.vnpttech.ssdc.nms.dao.hibernate.CounterDaoHibernate.java

License:Apache License

@Override
public Map searchCounter(Integer vanId, Integer materialsId, String type, Date startDate, Date endDate,
        Integer start, Integer limit) {
    try {/*from w w w .  j  a v a 2s .  co m*/
        Long starttime = System.currentTimeMillis();

        Long total = 0L;
        Map pagingMap = new HashMap();

        List<Counter> listCounter = new ArrayList<Counter>();
        Criteria criteria = getSession().createCriteria(Counter.class);
        Criteria criteria2 = getSession().createCriteria(Counter.class).setProjection(Projections.rowCount());

        //            criteria2.setProjection(Projections.rowCount());
        //Van
        if (vanId != null) {
            criteria.add(Restrictions.eq("van.id", vanId));
            criteria2.add(Restrictions.eq("van.id", vanId));
        }
        //Materials 
        if (materialsId != null) {
            criteria.add(Restrictions.eq("materials.id", materialsId));
            criteria2.add(Restrictions.eq("materials.id", materialsId));
        }
        //Type
        if (StringUtils.isNotBlank(type)) {
            criteria.add(Restrictions.like("type", type.trim(), MatchMode.ANYWHERE).ignoreCase());
            criteria2.add(Restrictions.like("type", type.trim(), MatchMode.ANYWHERE).ignoreCase());
        }
        //startDate
        if (startDate != null) {
            criteria.add(Restrictions.gt("incomingDate", startDate));
            criteria2.add(Restrictions.gt("incomingDate", startDate));
        }
        //endDate
        if (endDate != null) {
            criteria.add(Restrictions.lt("incomingDate", endDate));
            criteria2.add(Restrictions.lt("incomingDate", endDate));
        }
        criteria.addOrder(Order.desc("incomingDate"));
        //            criteria2.addOrder(Order.desc("incomingDate"));

        //Paging
        if (limit != null && limit > 0) {
            criteria.setFirstResult(start.intValue());
            criteria.setMaxResults(limit.intValue());
        }

        total = (Long) criteria2.uniqueResult();
        listCounter = criteria.list();
        pagingMap.put("list", listCounter);
        pagingMap.put("totalCount", total);

        log.debug("Time to search van: " + (System.currentTimeMillis() - starttime) + " (ms)");
        return pagingMap;
    } catch (Exception ex) {
        log.error("ERROR searchCounter: ", ex);
        return null;
    }
}