List of usage examples for javax.persistence.criteria Root get
<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);
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/* ww w. jav a2s. c om*/ 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:cc.sion.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 . ja 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]); } // if(log.isDebugEnabled()){ // log.debug(" {}-{}-{}",names[0],filter.operator,filter.value); // } // logic operator switch (filter.operator) { case EQ: predicates.add(builder.equal(expression, filter.value)); break; case NE: predicates.add(builder.notEqual(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/* w w w .jav a2 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; 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:com.netflix.genie.server.repository.jpa.ClusterSpecs.java
/** * Get all the clusters given the specified parameters. * * @param clusterCriteria The cluster criteria * @param commandCriteria The command Criteria * @return The specification/*from w w w.j a v a2s .c o m*/ */ public static Specification<Cluster> findByClusterAndCommandCriteria(final ClusterCriteria clusterCriteria, final Set<String> commandCriteria) { return new Specification<Cluster>() { @Override public Predicate toPredicate(final Root<Cluster> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) { final List<Predicate> predicates = new ArrayList<>(); final Join<Cluster, Command> commands = root.join(Cluster_.commands); cq.distinct(true); predicates.add(cb.equal(commands.get(Command_.status), CommandStatus.ACTIVE)); predicates.add(cb.equal(root.get(Cluster_.status), ClusterStatus.UP)); if (commandCriteria != null) { for (final String tag : commandCriteria) { predicates.add(cb.isMember(tag, commands.get(Command_.tags))); } } if (clusterCriteria != null) { for (final String tag : clusterCriteria.getTags()) { predicates.add(cb.isMember(tag, root.get(Cluster_.tags))); } } return cb.and(predicates.toArray(new Predicate[predicates.size()])); } }; }
From source file:com.shz.foundation.service.dynamic.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 . jav a 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: //?? Object value = filter.value; if (isBoolean(value)) { value = Boolean.parseBoolean(value.toString()); } predicates.add(builder.equal(expression, 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(); } private boolean isBoolean(Object value) { String valueString = value.toString().toLowerCase(); if (valueString.equals("true") || valueString.equals("false")) return true; return false; } }; }
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Creates a query for retrieving the IDs of the entities that match the provided filter * /* www . j av a 2s. co m*/ * @param entityManager * the entity manager * @param entityClass * the entity class * @param filter * the filter to apply * @param sortOrder * the sorting to apply * @return */ public static <T> CriteriaQuery<Tuple> createIdQuery(EntityManager entityManager, Class<T> entityClass, Filter filter, SortOrder... sortOrders) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Tuple> cq = builder.createTupleQuery(); Root<T> root = cq.from(entityClass); // select only the ID cq.multiselect(root.get(DynamoConstants.ID)); // Set where clause Predicate p = createPredicate(filter, builder, root); if (p != null) { cq.where(p); } // add order clause - this is also important in case of an ID query // since we do need to return the correct IDs! return addSortInformation(builder, cq, root, sortOrders); }
From source file:com.netflix.genie.server.repository.jpa.JobSpecs.java
/** * Find jobs that are zombies./* w ww . ja v a2s .c o m*/ * * @param currentTime The current time * @param zombieTime The time that zombies should be marked by * @return The specification for this query */ public static Specification<Job> findZombies(final long currentTime, final long zombieTime) { return new Specification<Job>() { @Override public Predicate toPredicate(final Root<Job> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) { // the equivalent query is as follows: // update Job set status='FAILED', finishTime=$max, exitCode=$zombie_code, // statusMsg='Job has been marked as a zombie' // where updateTime < $min and (status='RUNNING' or status='INIT')" final List<Predicate> predicates = new ArrayList<>(); predicates.add(cb.lessThan(root.get(Job_.updated), new Date(currentTime - zombieTime))); final Predicate orPredicate1 = cb.equal(root.get(Job_.status), JobStatus.RUNNING); final Predicate orPredicate2 = cb.equal(root.get(Job_.status), JobStatus.INIT); predicates.add(cb.or(orPredicate1, orPredicate2)); return cb.and(predicates.toArray(new Predicate[predicates.size()])); } }; }
From source file:com.netflix.genie.web.data.repositories.jpa.specifications.JpaCommandSpecs.java
/** * Get all the clusters given the specified parameters. * * @param applicationId The id of the application that is registered with these commands * @param statuses The status of the commands * @return The specification//from w w w . ja v a 2 s . c o m */ public static Specification<CommandEntity> findCommandsForApplication(final String applicationId, @Nullable final Set<CommandStatus> statuses) { return (final Root<CommandEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> { final List<Predicate> predicates = new ArrayList<>(); final Join<CommandEntity, ApplicationEntity> application = root.join(CommandEntity_.applications); predicates.add(cb.equal(application.get(ApplicationEntity_.uniqueId), applicationId)); if (statuses != null && !statuses.isEmpty()) { predicates.add( cb.or(statuses.stream().map(status -> cb.equal(root.get(CommandEntity_.status), status)) .toArray(Predicate[]::new))); } return cb.and(predicates.toArray(new Predicate[predicates.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/*from w w w . jav a 2s. 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; 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.faceye.feature.repository.jpa.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override/*w w w .j a va 2 s .co m*/ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (CollectionUtils.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: if (filter.value instanceof Number) { predicates.add(builder.equal(expression, (Number) filter.value)); } else if (filter.value instanceof String) { if (StringUtils.isNotEmpty(filter.value.toString())) { predicates.add(builder.equal(expression, filter.value)); } } else { 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 ISTRUE: predicates.add(builder.isTrue(expression)); break; case ISFALSE: predicates.add(builder.isFalse(expression)); break; case ISEMPTY: predicates.add(builder.isEmpty(expression)); break; case ISNULL: predicates.add(builder.isNull(expression)); break; case NE: predicates.add(builder.notEqual(expression, filter.value)); } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } return builder.conjunction(); } }; }