Example usage for javax.persistence.criteria CriteriaBuilder lessThan

List of usage examples for javax.persistence.criteria CriteriaBuilder lessThan

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder lessThan.

Prototype

<Y extends Comparable<? super Y>> Predicate lessThan(Expression<? extends Y> x, Y y);

Source Link

Document

Create a predicate for testing whether the first argument is less than the second.

Usage

From source file:com.atschx.summer.core.persistence.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override//from   w  w  w  . j  a  va2 s.  c om
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (Collections3.isNotEmpty(filters)) {

                List<Predicate> predicates = Lists.newArrayList();
                for (SearchFilter filter : filters) {
                    // nested path translate, Task??"user.name"filedName, ?Task.user.name
                    String[] names = StringUtils.split(filter.fieldName, ".");
                    Path expression = root.get(names[0]);
                    for (int i = 1; i < names.length; i++) {
                        expression = expression.get(names[i]);
                    }

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(expression, filter.value));
                        break;
                    case LIKE:
                        predicates.add(builder.like(expression, "%" + filter.value + "%"));
                        break;
                    case GT:
                        predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                        break;
                    case LT:
                        predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                        break;
                    case GTE:
                        predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case LTE:
                        predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    }
                }

                // ? and ???
                if (!predicates.isEmpty()) {
                    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            }

            return builder.conjunction();
        }
    };
}

From source file:com.the3.base.repository.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override//w w w. ja  v  a2  s.c o  m
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (filters != null && !filters.isEmpty()) {

                List<Predicate> predicates = Lists.newArrayList();
                for (SearchFilter filter : filters) {
                    // nested path translate, Task??"user.name"filedName, ?Task.user.name
                    String[] names = StringUtils.split(filter.fieldName, ".");
                    Path expression = root.get(names[0]);
                    for (int i = 1; i < names.length; i++) {
                        expression = expression.get(names[i]);
                    }

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(expression, filter.value));
                        break;
                    case LIKE:
                        predicates.add(builder.like(expression, "%" + filter.value + "%"));
                        break;
                    case GT:
                        predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                        break;
                    case LT:
                        predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                        break;
                    case GTE:
                        predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case LTE:
                        predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    }
                }

                // ? and ???
                if (!predicates.isEmpty()) {
                    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            }

            return builder.conjunction();
        }
    };
}

From source file:org.echoice.modules.persistence.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override/*from   w ww .  j a v a  2 s .  c  o  m*/
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (filters != null && filters.size() > 0) {

                List<Predicate> predicates = Lists.newArrayList();
                for (SearchFilter filter : filters) {
                    // nested path translate, Task??"user.name"filedName, ?Task.user.name
                    String[] names = StringUtils.split(filter.fieldName, ".");
                    Path expression = root.get(names[0]);
                    for (int i = 1; i < names.length; i++) {
                        expression = expression.get(names[i]);
                    }

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(expression, filter.value));
                        break;
                    case LIKE:
                        predicates.add(builder.like(expression, "%" + filter.value + "%"));
                        break;
                    case GT:
                        predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                        break;
                    case LT:
                        predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                        break;
                    case GTE:
                        predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case LTE:
                        predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    }
                }

                // ? and ???
                if (!predicates.isEmpty()) {
                    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            }

            return builder.conjunction();
        }
    };
}

From source file:com.i5le.framwork.core.persistence.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @Override/*from   w w  w .j a v a 2s  .  co  m*/
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (Collections3.isNotEmpty(filters)) {

                List<Predicate> predicates = Lists.newArrayList();
                for (SearchFilter filter : filters) {
                    // nested path translate, Task??"user.name"filedName, ?Task.user.name
                    String[] names = StringUtils.split(filter.fieldName, ".");
                    Path expression = root.get(names[0]);
                    for (int i = 1; i < names.length; i++) {
                        expression = expression.get(names[i]);
                    }

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(expression, filter.value));
                        break;
                    case LIKE:
                        predicates.add(builder.like(expression, "%" + filter.value + "%"));
                        break;
                    case GT:
                        predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                        break;
                    case LT:
                        predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                        break;
                    case GTE:
                        predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case LTE:
                        predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                        break;

                    case NEQ:
                        predicates.add(builder.notEqual(expression, filter.value));
                        break;

                    }
                }

                // ? and ???
                if (!predicates.isEmpty()) {
                    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            }

            return builder.conjunction();
        }
    };
}

From source file:net.carinae.dev.async.dao.QueuedTaskHolderDaoJPA2.java

@Override
public QueuedTaskHolder findRandomStalledTask() {

    Calendar TOO_LONG_AGO = Calendar.getInstance();
    TOO_LONG_AGO.add(Calendar.SECOND, -7200);

    // select qth from QueuedTask where 
    //      qth.startedStamp != null AND
    //      qth.startedStamp < TOO_LONG_AGO
    CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
    CriteriaQuery<QueuedTaskHolder> cq = cb.createQuery(QueuedTaskHolder.class);
    Root<QueuedTaskHolder> qth = cq.from(QueuedTaskHolder.class);
    cq.select(qth).where(cb.and(cb.isNull(qth.get(QueuedTaskHolder_.completedStamp)),
            cb.lessThan(qth.get(QueuedTaskHolder_.startedStamp), TOO_LONG_AGO)));

    List<QueuedTaskHolder> stalledTasks = this.entityManager.createQuery(cq).getResultList();

    if (stalledTasks.isEmpty()) {
        return null;
    } else {/*from  w ww .j ava  2  s . c  o m*/
        Random rand = new Random(System.currentTimeMillis());
        return stalledTasks.get(rand.nextInt(stalledTasks.size()));
    }

}

From source file:com.frame.base.repository.jpa.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        @SuppressWarnings({ "rawtypes", "unchecked" })
        @Override/* w ww .j  a v a  2s  . c  om*/
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (filters != null && !filters.isEmpty()) {
                List<Predicate> predicates = Lists.newArrayList();
                for (SearchFilter filter : filters) {
                    // nested path translate, Task??"user.name"filedName,
                    // ?Task.user.name
                    String[] names = StringUtils.split(filter.fieldName, ".");
                    Path expression = root.get(names[0]);
                    for (int i = 1; i < names.length; i++) {
                        expression = expression.get(names[i]);
                    }

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(expression, filter.value));
                        break;
                    case LIKE:
                        predicates.add(builder.like(expression, "%" + filter.value + "%"));
                        break;
                    case GT:
                        predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                        break;
                    case LT:
                        predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                        break;
                    case GTE:
                        predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case LTE:
                        predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case NE:
                        predicates.add(builder.notEqual(expression, (Comparable) filter.value));
                    case IN:
                        predicates.add(
                                builder.in(expression).in(Arrays.asList(filter.value.toString().split(","))));
                    }
                }

                // ? and ???
                if (!predicates.isEmpty()) {
                    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            }

            return builder.conjunction();
        }
    };
}

From source file:com.bxf.hradmin.common.utils.QueryParameterTransformer.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static Predicate generatePredicate(Root root, CriteriaBuilder builder,
        QueryParameter... queryParameters) {
    Predicate condition = builder.conjunction();
    for (QueryParameter queryParameter : queryParameters) {
        Object value = queryParameter.getValue();
        if (value == null || StringUtils.isBlank(value.toString())) {
            continue;
        }//from ww  w .  j  a  v a 2s.  c  o  m
        Path path = root.get(queryParameter.getKey());
        switch (queryParameter.getMode()) {
        case BETWEEN:
            Object[] values = asArray(value);
            if (values != null) {
                condition = builder.and(builder.between((Path<Comparable>) path, asComparable(values[0]),
                        asComparable(values[1])));
            }
            break;
        case GREATER_THAN:
            condition = builder.and(condition,
                    builder.greaterThan((Path<Comparable>) path, asComparable(value)));
            break;
        case GREATER_EQUALS:
            condition = builder.and(condition,
                    builder.greaterThanOrEqualTo((Path<Comparable>) path, asComparable(value)));
            break;
        case LESS_THAN:
            condition = builder.and(condition, builder.lessThan((Path<Comparable>) path, asComparable(value)));
            break;
        case LESS_EQUALS:
            condition = builder.and(condition,
                    builder.lessThanOrEqualTo((Path<Comparable>) path, asComparable(value)));
            break;
        case IS_NULL:
            condition = builder.and(condition, builder.isNull(path));
            break;
        case IS_NOT_NULL:
            condition = builder.and(condition, builder.isNotNull(path));
            break;
        case IN:
            condition = builder.and(condition, path.in(asArray(value)));
            break;
        case NOT_IN:
            condition = builder.and(condition, builder.not(path.in(asArray(value))));
            break;
        case LIKE:
            condition = builder.and(condition, builder.like(path, "%" + String.valueOf(value) + "%"));
            break;
        case NOT_LIKE:
            condition = builder.and(condition, builder.notLike(path, "%" + String.valueOf(value) + "%"));
            break;
        case EQUALS:
            condition = builder.and(condition, builder.equal(path, value));
            break;
        case NOT_EQUALS:
            condition = builder.and(condition, builder.notEqual(path, value));
            break;
        default:
            break;
        }
    }
    return condition;
}

From source file:com.it.j2ee.modules.common.util.DynamicSpecifications.java

public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters,
        final Class<T> entityClazz) {
    return new Specification<T>() {
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            if (Collections3.isNotEmpty(filters)) {

                List<Predicate> predicates = Lists.newArrayList();
                for (SearchFilter filter : filters) {
                    // nested path translate, Task??"user.name"filedName,
                    // ?Task.user.name
                    String[] names = StringUtils.split(filter.fieldName, ".");
                    Path expression = root.get(names[0]);
                    for (int i = 1; i < names.length; i++) {
                        expression = expression.get(names[i]);
                    }//ww w . j a  v a  2s.co m

                    // logic operator
                    switch (filter.operator) {
                    case EQ:
                        predicates.add(builder.equal(expression, filter.value));
                        break;
                    case LIKE:
                        predicates.add(builder.like(expression, "%" + filter.value + "%"));
                        break;
                    case GT:
                        predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
                        break;
                    case LT:
                        predicates.add(builder.lessThan(expression, (Comparable) filter.value));
                        break;
                    case GTE:
                        predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case LTE:
                        predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
                        break;
                    case IN:
                        CriteriaBuilder.In in = builder.in(expression);
                        String values = (String) filter.value;
                        String[] value = values.split(",");
                        for (int i = 0; i < value.length; i++) {
                            in.value(value[i]);
                        }
                        predicates.add(in);
                        break;
                    case NEQ:
                        predicates.add(builder.notEqual(expression, (Comparable) filter.value));
                        break;
                    }
                }

                // ? and ???
                if (!predicates.isEmpty()) {
                    return builder.and(predicates.toArray(new Predicate[predicates.size()]));
                }
            }

            return builder.conjunction();
        }
    };
}

From source file:com.mxep.web.common.persistence.DynamicSpecifications.java

private static List<Predicate> getPredicate(Collection<SearchFilter> filters, Root<?> root,
        CriteriaQuery<?> query, CriteriaBuilder builder) {
    List<Predicate> predicates = Lists.newArrayList();
    for (SearchFilter filter : filters) {
        // nested path translate, Task??"user.name"filedName,
        // ?Task.user.name
        String[] names = StringUtils.split(filter.fieldName, ".");
        Path expression = root.get(names[0]);
        for (int i = 1; i < names.length; i++) {
            expression = expression.get(names[i]);
        }//from   w w w  .  jav a2 s . c o m

        /*????*/
        if (expression.getJavaType().equals(Integer.class)
                && !filter.operator.equals(SearchFilter.Operator.IN)) {
            filter.value = Integer.parseInt(filter.value.toString());
        }

        // logic operator
        switch (filter.operator) {
        case EQ:
            predicates.add(builder.equal(expression, filter.value));
            break;
        case LIKE:
            predicates.add(builder.like(expression, "%" + filter.value + "%"));
            break;
        case GT:
            predicates.add(builder.greaterThan(expression, (Comparable) filter.value));
            break;
        case LT:
            predicates.add(builder.lessThan(expression, (Comparable) filter.value));
            break;
        case GTE:
            predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value));
            break;
        case LTE:
            predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value));
            break;
        case NEQ:
            predicates.add(builder.notEqual(expression, filter.value));
            break;
        case IN:
            In in = builder.in(expression);
            String value = filter.value.toString();
            String[] values = StringUtils.split(value, ",");
            for (int i = 0; i < values.length; i++) {
                /*????*/
                if (expression.getJavaType().equals(Integer.class) || expression.getJavaType().equals(int.class)
                        || expression.getJavaType().equals(byte.class)) {
                    in.value(Integer.valueOf(values[i]));
                } else {
                    in.value(values[i]);
                }

            }
            predicates.add(in);
            break;
        case NULL:
            predicates.add(builder.isNull(expression));
            break;
        case NOTNULL:
            predicates.add(builder.isNotNull(expression));
            break;
        }
    }
    return predicates;
}

From source file:net.carinae.dev.async.dao.QueuedTaskHolderDaoJPA2.java

@Override
public QueuedTaskHolder findNextTaskForExecution() {

    Calendar NOW = Calendar.getInstance();

    // select qt from QueuedTask where
    //      qt.startedStamp == null AND
    //      (qth.triggerStamp == null || qth.triggerStamp < NOW)
    // order by qth.version ASC, qt.creationStamp ASC
    CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();
    CriteriaQuery<QueuedTaskHolder> cq = cb.createQuery(QueuedTaskHolder.class);
    Root<QueuedTaskHolder> qth = cq.from(QueuedTaskHolder.class);
    cq.select(qth)/* ww w .  j  a  v a2s  .  c o  m*/
            .where(cb.and(cb.isNull(qth.get(QueuedTaskHolder_.startedStamp)),
                    cb.or(cb.isNull(qth.get(QueuedTaskHolder_.triggerStamp)),
                            cb.lessThan(qth.get(QueuedTaskHolder_.triggerStamp), NOW))))
            .orderBy(cb.asc(qth.get(QueuedTaskHolder_.version)),
                    cb.asc(qth.get(QueuedTaskHolder_.creationStamp)));

    List<QueuedTaskHolder> results = this.entityManager.createQuery(cq).setMaxResults(1).getResultList();
    if (results.isEmpty()) {
        return null;
    } else {
        return results.get(0);
    }

}