List of usage examples for javax.persistence.criteria CriteriaBuilder like
Predicate like(Expression<String> x, String pattern);
From source file:net.groupbuy.dao.impl.ArticleDaoImpl.java
public Page<Article> findPage(ArticleCategory articleCategory, List<Tag> tags, Pageable pageable) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root);//w w w .j ava2s . c om Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), true)); if (articleCategory != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.equal(root.get("articleCategory"), articleCategory), criteriaBuilder.like(root.get("articleCategory").<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); } if (tags != null && !tags.isEmpty()) { Subquery<Article> subquery = criteriaQuery.subquery(Article.class); Root<Article> subqueryRoot = subquery.from(Article.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.equal(subqueryRoot, root), subqueryRoot.join("tags").in(tags)); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(subquery)); } criteriaQuery.where(restrictions); criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop"))); return super.findPage(criteriaQuery, pageable); }
From source file:net.groupbuy.dao.impl.ArticleDaoImpl.java
public List<Article> findList(ArticleCategory articleCategory, List<Tag> tags, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root);/*from w w w .j av a2 s . c o m*/ Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), true)); if (articleCategory != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.equal(root.get("articleCategory"), articleCategory), criteriaBuilder.like(root.get("articleCategory").<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); } if (tags != null && !tags.isEmpty()) { Subquery<Article> subquery = criteriaQuery.subquery(Article.class); Root<Article> subqueryRoot = subquery.from(Article.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.equal(subqueryRoot, root), subqueryRoot.join("tags").in(tags)); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(subquery)); } criteriaQuery.where(restrictions); criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop"))); return super.findList(criteriaQuery, null, count, filters, orders); }
From source file:net.groupbuy.dao.impl.ArticleDaoImpl.java
public List<Article> findList(ArticleCategory articleCategory, Date beginDate, Date endDate, Integer first, Integer count) {//from ww w. j a v a2 s .c o m CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), true)); if (articleCategory != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.equal(root.get("articleCategory"), articleCategory), criteriaBuilder.like(root.get("articleCategory").<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); } if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate)); } if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate)); } criteriaQuery.where(restrictions); criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop"))); return super.findList(criteriaQuery, first, count, null, null); }
From source file:com.sixsq.slipstream.persistence.Run.java
private static Predicate viewListCommonQueryOptions(CriteriaBuilder builder, Root<Run> rootQuery, User user, String moduleResourceUri, String cloudServiceName) { Predicate where = null;/*from w w w . ja va 2 s . c om*/ if (!user.isSuper()) { where = andPredicate(builder, where, builder.equal(rootQuery.get("user_"), user.getName())); } if (moduleResourceUri != null && !"".equals(moduleResourceUri)) { where = andPredicate(builder, where, builder.equal(rootQuery.get("moduleResourceUri"), moduleResourceUri)); } if (cloudServiceName != null && !"".equals(cloudServiceName)) { // TODO: Replace the 'like' by an 'equals' where = andPredicate(builder, where, builder.like(rootQuery.<String>get("cloudServiceNames"), "%" + cloudServiceName + "%")); } return where; }
From source file:utils.jpa.EntityResource.java
private Query createCommonQuery(CriteriaBuilder cb, CriteriaQuery cq, Root root, TableSearchQuery tb, List<String> fieldsSearchBy) { List<Predicate> orPredicates = new ArrayList<>(); List<Predicate> andPredicates = new ArrayList<>(); //setting where conditions if (fieldsSearchBy != null && !fieldsSearchBy.isEmpty()) { for (String field : fieldsSearchBy) { if (tb.getFilter() != null && !tb.getFilter().isEmpty()) { orPredicates.add(cb.like(cb.lower(cb.concat(root.<String>get(field), "::text")), "%" + tb.getFilter().toLowerCase() + "%")); }//from w ww . j av a2s . c o m } } else { List<Field> filterColumns = SearchFields.getAll(entityClass); for (Field field : filterColumns) { EntityFilter entityFilter = tb.getFilterParameters().get(field.getName()); if ((Collection.class.isAssignableFrom(field.getType()) || List.class.isAssignableFrom(field.getType())) && (entityFilter == null || !entityFilter.getEntityFilterType().equals(EntityFilterType.EMPTY))) { continue; } if (entityFilter == null) { if (tb.getFilter() != null && !tb.getFilter().isEmpty()) { orPredicates.add(cb.like(cb.lower(cb.concat(root.<String>get(field.getName()), "::text")), "%" + tb.getFilter().toLowerCase() + "%")); } continue; } switch (entityFilter.getEntityFilterType()) { case WILDCARD: andPredicates.add(cb.like(cb.lower(cb.concat(root.<String>get(field.getName()), "::text")), "%" + entityFilter.getValue().toLowerCase() + "%")); break; case EMPTY: if (Collection.class.isAssignableFrom(field.getType()) || List.class.isAssignableFrom(field.getType())) { andPredicates.add(entityFilter.getValue().equals("hide") ? cb.isEmpty(root.<Collection>get(field.getName())) : cb.isNotEmpty(root.<Collection>get(field.getName()))); } else { andPredicates.add( entityFilter.getValue().equals("hide") ? root.<Object>get(field.getName()).isNull() : root.<Object>get(field.getName()).isNotNull()); } break; case MIN_MAX: andPredicates.add(cb.greaterThanOrEqualTo(root.<Date>get(field.getName()), new Date(Long.valueOf(entityFilter.getValues().get(0))))); andPredicates.add(cb.lessThanOrEqualTo(root.<Date>get(field.getName()), new Date(Long.valueOf(entityFilter.getValues().get(1))))); break; case MIN: andPredicates.add(cb.greaterThanOrEqualTo(root.<Date>get(field.getName()), new Date(Long.valueOf(entityFilter.getValue())))); break; case MAX: andPredicates.add(cb.lessThanOrEqualTo(root.<Date>get(field.getName()), new Date(Long.valueOf(entityFilter.getValue())))); break; } } } if (orPredicates.size() > 0 && andPredicates.size() > 0) { cq.where(cb.or(cb.or(orPredicates.toArray(new Predicate[0])), cb.and(andPredicates.toArray(new Predicate[0])))); } else if (orPredicates.size() > 0) { cq.where(cb.or(orPredicates.toArray(new Predicate[0]))); } else if (andPredicates.size() > 0) { cq.where(cb.and(andPredicates.toArray(new Predicate[0]))); } return em.createQuery(cq); }
From source file:net.shopxx.dao.impl.ArticleDaoImpl.java
public List<Article> findList(ArticleCategory articleCategory, Tag tag, Boolean isPublication, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root);//from w w w . jav a 2 s . co m Predicate restrictions = criteriaBuilder.conjunction(); if (articleCategory != null) { Subquery<ArticleCategory> subquery = criteriaQuery.subquery(ArticleCategory.class); Root<ArticleCategory> subqueryRoot = subquery.from(ArticleCategory.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.or(criteriaBuilder.equal(subqueryRoot, articleCategory), criteriaBuilder.like(subqueryRoot.<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.in(root.get("articleCategory")).value(subquery)); } if (tag != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.join("tags"), tag)); } if (isPublication != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), isPublication)); } criteriaQuery.where(restrictions); if (orders == null || orders.isEmpty()) { criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")), criteriaBuilder.desc(root.get("createDate"))); } return super.findList(criteriaQuery, null, count, filters, orders); }
From source file:net.shopxx.dao.impl.ArticleDaoImpl.java
public Page<Article> findPage(ArticleCategory articleCategory, Tag tag, Boolean isPublication, Pageable pageable) {//from w w w. jav a 2 s . com CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); if (articleCategory != null) { Subquery<ArticleCategory> subquery = criteriaQuery.subquery(ArticleCategory.class); Root<ArticleCategory> subqueryRoot = subquery.from(ArticleCategory.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.or(criteriaBuilder.equal(subqueryRoot, articleCategory), criteriaBuilder.like(subqueryRoot.<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.in(root.get("articleCategory")).value(subquery)); } if (tag != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.join("tags"), tag)); } if (isPublication != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), isPublication)); } criteriaQuery.where(restrictions); if (pageable == null || ((StringUtils.isEmpty(pageable.getOrderProperty()) || pageable.getOrderDirection() == null) && CollectionUtils.isEmpty(pageable.getOrders()))) { criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")), criteriaBuilder.desc(root.get("createDate"))); } return super.findPage(criteriaQuery, pageable); }
From source file:net.shopxx.dao.impl.ArticleDaoImpl.java
public List<Article> findList(ArticleCategory articleCategory, Boolean isPublication, Article.GenerateMethod generateMethod, Date beginDate, Date endDate, Integer first, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root);/*from ww w . j av a2 s . c o m*/ Predicate restrictions = criteriaBuilder.conjunction(); if (articleCategory != null) { Subquery<ArticleCategory> subquery = criteriaQuery.subquery(ArticleCategory.class); Root<ArticleCategory> subqueryRoot = subquery.from(ArticleCategory.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.or(criteriaBuilder.equal(subqueryRoot, articleCategory), criteriaBuilder.like(subqueryRoot.<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.in(root.get("articleCategory")).value(subquery)); } if (isPublication != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), isPublication)); } if (generateMethod != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("generateMethod"), generateMethod)); } if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate)); } if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate)); } criteriaQuery.where(restrictions); return super.findList(criteriaQuery, first, count, null, null); }
From source file:com.zero.dao.impl.BaseDaoImpl.java
private void addRestrictions(CriteriaQuery<T> criteriaQuery, Pageable pageable) { if (criteriaQuery == null || pageable == null) { return;/*w w w .jav a 2 s .c o m*/ } Root<T> root = getRoot(criteriaQuery); if (root == null) { return; } CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); Predicate restrictions = criteriaQuery.getRestriction() != null ? criteriaQuery.getRestriction() : criteriaBuilder.conjunction(); if (StringUtils.isNotEmpty(pageable.getSearchProperty()) && StringUtils.isNotEmpty(pageable.getSearchValue())) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder .like(root.<String>get(pageable.getSearchProperty()), "%" + pageable.getSearchValue() + "%")); } if (pageable.getFilters() != null) { for (Filter filter : pageable.getFilters()) { if (filter == null || StringUtils.isEmpty(filter.getProperty())) { continue; } if (filter.getOperator() == Operator.eq && filter.getValue() != null) { if (filter.getIgnoreCase() != null && filter.getIgnoreCase() && filter.getValue() instanceof String) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(criteriaBuilder.lower(root.<String>get(filter.getProperty())), ((String) filter.getValue()).toLowerCase())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get(filter.getProperty()), filter.getValue())); } } else if (filter.getOperator() == Operator.ne && filter.getValue() != null) { if (filter.getIgnoreCase() != null && filter.getIgnoreCase() && filter.getValue() instanceof String) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.notEqual( criteriaBuilder.lower(root.<String>get(filter.getProperty())), ((String) filter.getValue()).toLowerCase())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.notEqual(root.get(filter.getProperty()), filter.getValue())); } } else if (filter.getOperator() == Operator.gt && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.gt(root.<Number>get(filter.getProperty()), (Number) filter.getValue())); } else if (filter.getOperator() == Operator.lt && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lt(root.<Number>get(filter.getProperty()), (Number) filter.getValue())); } else if (filter.getOperator() == Operator.ge && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get(filter.getProperty()), (Number) filter.getValue())); } else if (filter.getOperator() == Operator.le && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get(filter.getProperty()), (Number) filter.getValue())); } else if (filter.getOperator() == Operator.like && filter.getValue() != null && filter.getValue() instanceof String) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder .like(root.<String>get(filter.getProperty()), (String) filter.getValue())); } else if (filter.getOperator() == Operator.in && filter.getValue() != null) { restrictions = criteriaBuilder.and(restrictions, root.get(filter.getProperty()).in(filter.getValue())); } else if (filter.getOperator() == Operator.isNull) { restrictions = criteriaBuilder.and(restrictions, root.get(filter.getProperty()).isNull()); } else if (filter.getOperator() == Operator.isNotNull) { restrictions = criteriaBuilder.and(restrictions, root.get(filter.getProperty()).isNotNull()); } } } criteriaQuery.where(restrictions); }
From source file:com.netflix.genie.web.data.repositories.jpa.specifications.JpaJobSpecs.java
/** * Generate a criteria query predicate for a where clause based on the given parameters. * * @param root The root to use * @param cb The criteria builder to use * @param id The job id/*from www .jav a2 s . com*/ * @param name The job name * @param user The user who created the job * @param statuses The job statuses * @param tags The tags for the jobs to find * @param clusterName The cluster name * @param cluster The cluster the job should have been run on * @param commandName The command name * @param command The command the job should have been run with * @param minStarted The time which the job had to start after in order to be return (inclusive) * @param maxStarted The time which the job had to start before in order to be returned (exclusive) * @param minFinished The time which the job had to finish after in order to be return (inclusive) * @param maxFinished The time which the job had to finish before in order to be returned (exclusive) * @param grouping The job grouping to search for * @param groupingInstance The job grouping instance to search for * @return The specification */ @SuppressWarnings("checkstyle:parameternumber") public static Predicate getFindPredicate(final Root<JobEntity> root, final CriteriaBuilder cb, @Nullable final String id, @Nullable final String name, @Nullable final String user, @Nullable final Set<JobStatus> statuses, @Nullable final Set<String> tags, @Nullable final String clusterName, @Nullable final ClusterEntity cluster, @Nullable final String commandName, @Nullable final CommandEntity command, @Nullable final Instant minStarted, @Nullable final Instant maxStarted, @Nullable final Instant minFinished, @Nullable final Instant maxFinished, @Nullable final String grouping, @Nullable final String groupingInstance) { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(id)) { predicates.add( JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.uniqueId), id)); } if (StringUtils.isNotBlank(name)) { predicates .add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.name), name)); } if (StringUtils.isNotBlank(user)) { predicates .add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.user), user)); } if (statuses != null && !statuses.isEmpty()) { final List<Predicate> orPredicates = statuses.stream() .map(status -> cb.equal(root.get(JobEntity_.status), status)).collect(Collectors.toList()); predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()]))); } if (tags != null && !tags.isEmpty()) { predicates.add( cb.like(root.get(JobEntity_.tagSearchString), JpaSpecificationUtils.getTagLikeString(tags))); } if (cluster != null) { predicates.add(cb.equal(root.get(JobEntity_.cluster), cluster)); } if (StringUtils.isNotBlank(clusterName)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.clusterName), clusterName)); } if (command != null) { predicates.add(cb.equal(root.get(JobEntity_.command), command)); } if (StringUtils.isNotBlank(commandName)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.commandName), commandName)); } if (minStarted != null) { predicates.add(cb.greaterThanOrEqualTo(root.get(JobEntity_.started), minStarted)); } if (maxStarted != null) { predicates.add(cb.lessThan(root.get(JobEntity_.started), maxStarted)); } if (minFinished != null) { predicates.add(cb.greaterThanOrEqualTo(root.get(JobEntity_.finished), minFinished)); } if (maxFinished != null) { predicates.add(cb.lessThan(root.get(JobEntity_.finished), maxFinished)); } if (grouping != null) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.grouping), grouping)); } if (groupingInstance != null) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(JobEntity_.groupingInstance), groupingInstance)); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); }