Example usage for javax.persistence.criteria Path in

List of usage examples for javax.persistence.criteria Path in

Introduction

In this page you can find the example usage for javax.persistence.criteria Path in.

Prototype

Predicate in(Object... values);

Source Link

Document

Create a predicate to test whether the expression is a member of the argument list.

Usage

From source file:de.ks.idnadrev.information.view.InformationOverviewDS.java

protected List<Tag> getTags(List<String> tagNames, EntityManager em) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Tag> query = builder.createQuery(Tag.class);
    Root<Tag> root = query.from(Tag.class);
    Path<String> namePath = root.get(KEY_NAME);
    query.select(root);/*from ww  w  .  jav  a2  s  .c  om*/
    query.where(namePath.in(tagNames));

    return em.createQuery(query).getResultList();
}

From source file:com.dbs.sdwt.jpa.ByPropertySelectorUtil.java

private <E> void byStringSelector(Root<E> root, CriteriaBuilder builder, List<Predicate> predicates,
        SearchParameters sp, PropertySelector<? super E, String> selector) {
    if (selector.isNotEmpty()) {
        List<Predicate> selectorPredicates = newArrayList();
        if (SearchMode.IN == selector.getSearchMode()) {
            Path<String> path = jpaUtil.getPath(root, selector.getAttributes());
            predicates.add(path.in(selector.getSelected()));
        } else if (SearchMode.NOT_IN == selector.getSearchMode()) {
            Path<String> path = jpaUtil.getPath(root, selector.getAttributes());
            predicates.add(builder.not(path.in(selector.getSelected())));
        } else {/*from   ww w. j  a v a  2s  .c  o  m*/
            for (String selection : selector.getSelected()) {
                Path<String> path = jpaUtil.getPath(root, selector.getAttributes());
                selectorPredicates
                        .add(jpaUtil.stringPredicate(path, selection, selector.getSearchMode(), sp, builder));
                if (selector.isOrMode()) {
                    predicates.add(jpaUtil.orPredicate(builder, selectorPredicates));
                } else {
                    predicates.add(jpaUtil.andPredicate(builder, selectorPredicates));
                }
            }
        }

    }
}

From source file:cn.guoyukun.spring.jpa.repository.support.SimpleBaseRepository.java

public List<M> findAll(Iterable<ID> ids) {

    return getQuery(new Specification<M>() {
        public Predicate toPredicate(Root<M> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            Path<?> path = root.get(entityInformation.getIdAttribute());
            return path.in(cb.parameter(Iterable.class, "ids"));
        }//  w  w  w  .  j av  a  2s.  co m
    }, (Sort) null).setParameter("ids", ids).getResultList();
}

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;
        }/*w  w w.  j  a  v  a 2 s . 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.uni.dao.etc.UniJpaRepository.java

public List<T> findAll(Iterable<ID> ids) {

    return getQuery(new Specification<T>() {
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
            Path<?> path = root.get(entityInformation.getIdAttribute());
            return path.in(cb.parameter(Iterable.class, "ids"));
        }/*from www .  ja va 2s .c o  m*/
    }, (Sort) null).setParameter("ids", ids).getResultList();
}

From source file:org.oncoblocks.centromere.jpa.QueryCriteriaSpecification.java

public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
    String key = queryCriteria.getKey();
    Object value = queryCriteria.getValue();
    Evaluation eval = queryCriteria.getEvaluation();
    Path path = null;
    if (key.contains(".")) {
        String[] bits = key.split("\\.");
        path = root.join(bits[0]).get(bits[1]);
    } else {//w ww  . j  a va 2  s. co m
        path = root.get(key);
    }
    logger.debug(String.format("[CENTROMERE] Converting QueryCriteria to JPA specification: %s",
            queryCriteria.toString()));
    switch (eval) {
    case EQUALS:
        return criteriaBuilder.equal(path, value);
    case NOT_EQUALS:
        return criteriaBuilder.notEqual(path, value);
    case IN:
        return path.in(value);
    case NOT_IN:
        return criteriaBuilder.not(path.in(value));
    case IS_NULL:
        return criteriaBuilder.isNull(path);
    case NOT_NULL:
        return criteriaBuilder.isNotNull(path);
    case GREATER_THAN:
        return criteriaBuilder.greaterThan(path, value.toString());
    case GREATER_THAN_EQUALS:
        return criteriaBuilder.greaterThanOrEqualTo(path, value.toString());
    case LESS_THAN:
        return criteriaBuilder.lessThan(path, value.toString());
    case LESS_THAN_EQUALS:
        return criteriaBuilder.lessThanOrEqualTo(path, value.toString());
    case BETWEEN:
        return criteriaBuilder.and(criteriaBuilder.greaterThan(path, ((List<?>) value).get(0).toString()),
                criteriaBuilder.lessThan(path, ((List<?>) value).get(1).toString()));
    case OUTSIDE:
        return criteriaBuilder.or(criteriaBuilder.greaterThan(path, ((List<?>) value).get(1).toString()),
                criteriaBuilder.lessThan(path, ((List<?>) value).get(0).toString()));
    case BETWEEN_INCLUSIVE:
        return criteriaBuilder.and(
                criteriaBuilder.greaterThanOrEqualTo(path, ((List<?>) value).get(0).toString()),
                criteriaBuilder.lessThanOrEqualTo(path, ((List<?>) value).get(1).toString()));
    case OUTSIDE_INCLUSIVE:
        return criteriaBuilder.or(
                criteriaBuilder.greaterThanOrEqualTo(path, ((List<?>) value).get(1).toString()),
                criteriaBuilder.lessThanOrEqualTo(path, ((List<?>) value).get(0).toString()));
    case LIKE:
        return criteriaBuilder.like(path, "%" + value.toString() + "%");
    case NOT_LIKE:
        return criteriaBuilder.notLike(path, "%" + value.toString() + "%");
    case STARTS_WITH:
        return criteriaBuilder.like(path, value.toString() + "%");
    case ENDS_WITH:
        return criteriaBuilder.like(path, "%" + value.toString());
    default:
        return criteriaBuilder.equal(root.get(queryCriteria.getKey()), queryCriteria.getValue());
    }
}

From source file:com.jaxio.jpa.querybyexample.ByPropertySelectorUtil.java

private <E> void byObjectOrModeSelector(Root<E> root, CriteriaBuilder builder, List<Predicate> predicates,
        SearchParameters sp, PropertySelector<? super E, ?> selector) {
    List<Predicate> selectorPredicates = newArrayList();
    Path<?> path = jpaUtil.getPath(root, selector.getAttributes());
    List<?> selected = selector.getSelected();
    if (selected.contains(null)) {
        selected = newArrayList(selector.getSelected());
        selected.remove(null);//from w ww  .j a  v  a  2 s . c  o  m
        selectorPredicates.add(builder.isNull(path));
    }
    if (isNotEmpty(selected)) {
        if (selected.get(0) instanceof Identifiable) {
            List<Serializable> ids = newArrayList();
            for (Object selection : selected) {
                ids.add(((Identifiable<?>) selection).getId());
            }
            selectorPredicates.add(path.get("id").in(ids));
        } else {
            selectorPredicates.add(path.in(selected));
        }
    }
    predicates.add(jpaUtil.orPredicate(builder, selectorPredicates));
}

From source file:com.dbs.sdwt.jpa.ByPropertySelectorUtil.java

private <E> void byObjectOrModeSelector(Root<E> root, CriteriaBuilder builder, List<Predicate> predicates,
        SearchParameters sp, PropertySelector<? super E, ?> selector) {
    List<Predicate> selectorPredicates = newArrayList();
    Path<?> path = jpaUtil.getPath(root, selector.getAttributes());
    List<?> selected = selector.getSelected();
    if (selected.contains(null)) {
        selected = newArrayList(selector.getSelected());
        selected.remove(null);//  w  w  w.j  ava  2 s  .  c o  m
        selectorPredicates.add(builder.isNull(path));
    }
    if (isNotEmpty(selected)) {
        if (selected.get(0) instanceof Identifiable) {
            List<Serializable> ids = newArrayList();
            for (Object selection : selected) {
                ids.add(((Identifiable<?>) selection).getId());
            }
            if (SearchMode.NOT_IN == selector.getSearchMode()) {
                selectorPredicates.add(builder.not(path.get("id").in(ids)));
            } else {
                selectorPredicates.add(path.get("id").in(ids));
            }
        } else {
            if (SearchMode.NOT_IN == selector.getSearchMode()) {
                selectorPredicates.add(builder.not(path.in(selected)));
            } else {
                selectorPredicates.add(path.in(selected));
            }

        }
    }

    predicates.add(jpaUtil.orPredicate(builder, selectorPredicates));
}

From source file:org.broadleafcommerce.inventory.admin.server.service.handler.InventorySkuCustomPersistenceHandler.java

@Override
public void applyAdditionalFetchCriteria(List<FilterMapping> filterMappings, CriteriaTransferObject cto,
        PersistencePackage persistencePackage) {
    super.applyAdditionalFetchCriteria(filterMappings, cto, persistencePackage);
    // grab the fulfillment location off of the custom criteria from the
    // frontend//from  w w  w.j av a  2 s .c  o  m
    final Long locationId = Long.parseLong(persistencePackage.getCustomCriteria()[1]);
    FilterMapping f = new FilterMapping().withFieldPath(new FieldPath().withTargetProperty("id"))
            .withRestriction(new Restriction().withPredicateProvider(new PredicateProvider() {
                @Override
                public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder,
                        From root, String ceilingEntity, String fullPropertyName, Path explicitPath,
                        List directValues) {
                    // DetachedCriteria locationSkuIds1 =
                    // DetachedCriteria.forClass(InventoryImpl.class)
                    // .add(Restrictions.eq("fulfillmentLocation.id",
                    // locationId))
                    // .setProjection(Projections.property("sku.id"));
                    // return
                    // Subqueries.propertyNotIn(targetPropertyName,
                    // locationSkuIds);

                    CriteriaQuery<Long> q = builder.createQuery(Long.class);
                    Root<InventoryImpl> subRoot = q.from(InventoryImpl.class);
                    q.where(builder.equal(subRoot.get("fulfillmentLocation.id"), locationId));
                    q.select(subRoot.<Long>get("sku.id"));
                    return builder.not(explicitPath.in(q)); // FIXME
                }
            }));
    filterMappings.add(f);
}

From source file:org.finra.dm.dao.impl.BaseJpaDaoImpl.java

/**
 * Gets an "in" clause predicate for a list of values. This will take care of breaking the list of values into a group of sub-lists where each sub-list is
 * placed in a separate "in" clause and all "in" clauses are "or"ed together. The size of each sub-list is obtained through an environment configuration.
 *
 * @param builder the criteria builder.//from   w w  w .java  2  s . com
 * @param path the path to the field that is being filtered.
 * @param values the list of values to place in the in clause.
 * @param <T> the type referenced by the path.
 *
 * @return the predicate for the in clause.
 */
protected <T> Predicate getPredicateForInClause(CriteriaBuilder builder, Path<T> path, List<T> values) {
    // Get the chunk size from the environment and use a default as necessary.
    int inClauseChunkSize = configurationHelper.getProperty(ConfigurationValue.DB_IN_CLAUSE_CHUNK_SIZE,
            Integer.class);

    // Initializes the returned predicate and the value list size.
    Predicate predicate = null;
    int listSize = values.size();

    // Loop through each chunk of values until we have reached the end of the values.
    for (int i = 0; i < listSize; i += inClauseChunkSize) {
        // Get a sub-list for the current chunk of data.
        List<T> valuesSubList = values.subList(i,
                (listSize > (i + inClauseChunkSize) ? (i + inClauseChunkSize) : listSize));

        // Get an updated predicate which will be the "in" clause of the sub-list on the first loop or the "in" clause of the sub-list "or"ed with the\
        // previous sub-list "in" clause.
        predicate = (predicate == null ? path.in(valuesSubList)
                : builder.or(predicate, path.in(valuesSubList)));
    }

    // Return the "in" clause predicate.
    return predicate;
}