List of usage examples for javax.persistence.criteria CriteriaBuilder equal
Predicate equal(Expression<?> x, Object y);
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Create a query for fetching a single object * /*from w w w .j a va 2 s . c o m*/ * @param entityManager * the entity manager * @param entityClass * the entity class * @param id * ID of the object to return * @param fetchJoins * fetch joins to include * @return */ public static <ID, T> CriteriaQuery<T> createFetchSingleObjectQuery(EntityManager entityManager, Class<T> entityClass, ID id, FetchJoinInformation[] fetchJoins) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> cq = builder.createQuery(entityClass); Root<T> root = cq.from(entityClass); addFetchJoinInformation(root, fetchJoins); cq.where(builder.equal(root.get(DynamoConstants.ID), id)); return cq; }
From source file:com.netflix.genie.web.data.repositories.jpa.specifications.JpaApplicationSpecs.java
/** * Get a specification using the specified parameters. * * @param name The name of the application * @param user The name of the user who created the application * @param statuses The status of the application * @param tags The set of tags to search with * @param type The type of applications to fine * @return A specification object used for querying *//* www . ja v a 2 s . com*/ public static Specification<ApplicationEntity> find(@Nullable final String name, @Nullable final String user, @Nullable final Set<ApplicationStatus> statuses, @Nullable final Set<TagEntity> tags, @Nullable final String type) { return (final Root<ApplicationEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(ApplicationEntity_.name), name)); } if (StringUtils.isNotBlank(user)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(ApplicationEntity_.user), user)); } if (statuses != null && !statuses.isEmpty()) { predicates.add( cb.or(statuses.stream().map(status -> cb.equal(root.get(ApplicationEntity_.status), status)) .toArray(Predicate[]::new))); } if (tags != null && !tags.isEmpty()) { final Join<ApplicationEntity, TagEntity> tagEntityJoin = root.join(ApplicationEntity_.tags); predicates.add(tagEntityJoin.in(tags)); cq.groupBy(root.get(ApplicationEntity_.id)); cq.having(cb.equal(cb.count(root.get(ApplicationEntity_.id)), tags.size())); } if (StringUtils.isNotBlank(type)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(ApplicationEntity_.type), type)); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); }; }
From source file:com.netflix.genie.web.data.repositories.jpa.specifications.JpaCommandSpecs.java
/** * Get a specification using the specified parameters. * * @param name The name of the command * @param user The name of the user who created the command * @param statuses The status of the command * @param tags The set of tags to search the command for * @return A specification object used for querying *///from w ww . ja v a2s . c om public static Specification<CommandEntity> find(@Nullable final String name, @Nullable final String user, @Nullable final Set<CommandStatus> statuses, @Nullable final Set<TagEntity> tags) { return (final Root<CommandEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(CommandEntity_.name), name)); } if (StringUtils.isNotBlank(user)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(CommandEntity_.user), user)); } if (statuses != null && !statuses.isEmpty()) { predicates.add( cb.or(statuses.stream().map(status -> cb.equal(root.get(CommandEntity_.status), status)) .toArray(Predicate[]::new))); } if (tags != null && !tags.isEmpty()) { final Join<CommandEntity, TagEntity> tagEntityJoin = root.join(CommandEntity_.tags); predicates.add(tagEntityJoin.in(tags)); cq.groupBy(root.get(CommandEntity_.id)); cq.having(cb.equal(cb.count(root.get(CommandEntity_.id)), tags.size())); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); }; }
From source file:com.netflix.genie.core.jpa.specifications.JpaApplicationSpecs.java
/** * Get a specification using the specified parameters. * * @param name The name of the application * @param user The name of the user who created the application * @param statuses The status of the application * @param tags The set of tags to search the command for * @param type The type of applications to fine * @return A specification object used for querying */// w ww. j a va2 s . c om public static Specification<ApplicationEntity> find(final String name, final String user, final Set<ApplicationStatus> statuses, final Set<String> tags, final String type) { return (final Root<ApplicationEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(ApplicationEntity_.name), name)); } if (StringUtils.isNotBlank(user)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(ApplicationEntity_.user), user)); } if (statuses != null && !statuses.isEmpty()) { final List<Predicate> orPredicates = statuses.stream() .map(status -> cb.equal(root.get(ApplicationEntity_.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(ApplicationEntity_.tags), JpaSpecificationUtils.getTagLikeString(tags))); } if (StringUtils.isNotBlank(type)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(ApplicationEntity_.type), type)); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); }; }
From source file:com.netflix.genie.core.jpa.specifications.JpaCommandSpecs.java
/** * Get a specification using the specified parameters. * * @param name The name of the command * @param user The name of the user who created the command * @param statuses The status of the command * @param tags The set of tags to search the command for * @return A specification object used for querying *//*from ww w.j a v a 2 s . c o m*/ public static Specification<CommandEntity> find(final String name, final String user, final Set<CommandStatus> statuses, final Set<String> tags) { return (final Root<CommandEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(CommandEntity_.name), name)); } if (StringUtils.isNotBlank(user)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(CommandEntity_.user), user)); } if (statuses != null && !statuses.isEmpty()) { final List<Predicate> orPredicates = statuses.stream() .map(status -> cb.equal(root.get(CommandEntity_.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(CommandEntity_.tags), JpaSpecificationUtils.getTagLikeString(tags))); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); }; }
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a query used to retrieve a single entity based on a unique property value * /*from ww w . ja va 2 s.com*/ * @param entityManager * @param entityClass * @param propertyName * @param value * @return */ public static <T> CriteriaQuery<T> createUniquePropertyQuery(EntityManager entityManager, Class<T> entityClass, String propertyName, Object value, boolean caseSensitive) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> cq = builder.createQuery(entityClass); Root<T> root = cq.from(entityClass); Predicate equals = null; if (value instanceof String && !caseSensitive) { equals = builder.equal(builder.upper(root.get(propertyName).as(String.class)), ((String) value).toUpperCase()); } else { equals = builder.equal(root.get(propertyName), value); } cq.where(equals); return cq; }
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a query to fetch an object based on a value of a unique property * /* w w w . j a v a2 s. c o m*/ * @param entityManager * the entity manager * @param entityClass * the entity class * @param fetchJoins * the fetch joins to include * @param propertyName * name of the property to search on * @param value * value of the property to search on * @return */ public static <T> CriteriaQuery<T> createUniquePropertyFetchQuery(EntityManager entityManager, Class<T> entityClass, FetchJoinInformation[] fetchJoins, String propertyName, Object value, boolean caseSensitive) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> cq = builder.createQuery(entityClass); Root<T> root = cq.from(entityClass); addFetchJoinInformation(root, fetchJoins); Predicate equals = null; if (value instanceof String && !caseSensitive) { equals = builder.equal(builder.upper(root.get(propertyName).as(String.class)), ((String) value).toUpperCase()); } else { equals = builder.equal(root.get(propertyName), value); } cq.where(equals); cq.distinct(true); return cq; }
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 va 2 s . com*/ 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 ww . j a v a 2 s . 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; } } // ? 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/* w ww.java 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(); } }; }