List of usage examples for javax.persistence.criteria CriteriaBuilder isNull
Predicate isNull(Expression<?> x);
From source file:com.goodhuddle.huddle.repository.MemberSpecification.java
public static Specification<Member> search(final Huddle huddle, final SearchMembersRequest request) { return new Specification<Member>() { @Override/*from w ww. j a v a 2s.com*/ public Predicate toPredicate(Root<Member> member, CriteriaQuery<?> query, CriteriaBuilder builder) { Predicate conjunction = builder.conjunction(); // huddle conjunction.getExpressions().add(builder.equal(member.get("huddle"), huddle)); // keywords if (StringUtils.isNotBlank(request.getKeywords())) { String[] terms = StringUtils.split(request.getKeywords()); for (String keyword : terms) { if (keyword != null && keyword.length() > 0) { String matchTerm = "%" + keyword.toLowerCase() + "%"; conjunction.getExpressions().add(builder.or( builder.like(builder.lower(member.<String>get("username")), matchTerm), builder.like(builder.lower(member.<String>get("email")), matchTerm), builder.like(builder.lower(member.<String>get("firstName")), matchTerm), builder.like(builder.lower(member.<String>get("lastName")), matchTerm))); } } } // security groups if (CollectionUtils.isNotEmpty(request.getSecurityGroupIds())) { Join<Object, Object> securityGroup = member.join("securityGroup", request.isIncludeNoAccess() ? JoinType.LEFT : JoinType.INNER); Predicate disjunction = builder.disjunction(); for (Long id : request.getSecurityGroupIds()) { disjunction.getExpressions().add(builder.equal(securityGroup.get("id"), id)); } if (request.isIncludeNoAccess()) { disjunction.getExpressions().add(builder.isNull(securityGroup.get("id"))); } conjunction.getExpressions().add(disjunction); } else if (request.isIncludeNoAccess()) { conjunction.getExpressions().add(builder.isNull(member.get("securityGroup"))); } // tags MemberTagFilter tagFilter = request.getTags(); if (tagFilter != null) { if (tagFilter.getIncluded() != null) { if (CollectionUtils.isNotEmpty(tagFilter.getIncluded().getTagIds())) { MemberTagFilter.TagSet included = request.getTags().getIncluded(); MatchType matchType = included.getMatchType(); Predicate tagPredicate = matchType.equals(MatchType.all) ? builder.conjunction() : builder.disjunction(); for (Long tagId : included.getTagIds()) { Subquery<Member> sq = query.subquery(Member.class); Root<Member> subMember = sq.from(Member.class); Join<Member, Tag> tag = subMember.join("tags"); sq.select(subMember).where(builder.equal(tag.get("id"), tagId)); tagPredicate.getExpressions().add(builder.in(member).value(sq)); } conjunction.getExpressions().add(tagPredicate); } } if (tagFilter.getExcluded() != null) { if (CollectionUtils.isNotEmpty(tagFilter.getExcluded().getTagIds())) { MemberTagFilter.TagSet excluded = request.getTags().getExcluded(); MatchType matchType = excluded.getMatchType(); Predicate tagPredicate = matchType.equals(MatchType.all) ? builder.disjunction() : builder.conjunction(); for (Long tagId : excluded.getTagIds()) { Subquery<Member> sq = query.subquery(Member.class); Root<Member> subMember = sq.from(Member.class); Join<Member, Tag> tag = subMember.join("tags"); sq.select(subMember).where(builder.equal(tag.get("id"), tagId)); tagPredicate.getExpressions().add(builder.in(member).value(sq).not()); } conjunction.getExpressions().add(tagPredicate); } } } return conjunction; } }; }
From source file:net.groupbuy.dao.impl.ProductNotifyDaoImpl.java
public Long count(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class); Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class); criteriaQuery.select(root);/*from w ww. j a v a 2s .c om*/ Predicate restrictions = criteriaBuilder.conjunction(); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (isMarketable != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product").get("isMarketable"), isMarketable)); } if (isOutOfStock != null) { Path<Integer> stock = root.get("product").get("stock"); Path<Integer> allocatedStock = root.get("product").get("allocatedStock"); if (isOutOfStock) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock), criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock)); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock), criteriaBuilder.greaterThan(stock, allocatedStock))); } } if (hasSent != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent)); } criteriaQuery.where(restrictions); return super.count(criteriaQuery, null); }
From source file:net.groupbuy.dao.impl.ProductNotifyDaoImpl.java
public Page<ProductNotify> findPage(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent, Pageable pageable) {//from ww w . j av a 2s . co m CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class); Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (isMarketable != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product").get("isMarketable"), isMarketable)); } if (isOutOfStock != null) { Path<Integer> stock = root.get("product").get("stock"); Path<Integer> allocatedStock = root.get("product").get("allocatedStock"); if (isOutOfStock) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock), criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock)); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock), criteriaBuilder.greaterThan(stock, allocatedStock))); } } if (hasSent != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent)); } criteriaQuery.where(restrictions); return super.findPage(criteriaQuery, pageable); }
From source file:net.shopxx.dao.impl.ProductNotifyDaoImpl.java
public Long count(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class); Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class); criteriaQuery.select(root);//from w ww . j av a 2 s .co m Predicate restrictions = criteriaBuilder.conjunction(); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (isMarketable != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product").get("goods").get("isMarketable"), isMarketable)); } if (isOutOfStock != null) { Path<Integer> stock = root.get("product").get("stock"); Path<Integer> allocatedStock = root.get("product").get("allocatedStock"); if (isOutOfStock) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock), criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock)); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock), criteriaBuilder.greaterThan(stock, allocatedStock))); } } if (hasSent != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent)); } criteriaQuery.where(restrictions); return super.count(criteriaQuery, null); }
From source file:net.shopxx.dao.impl.ProductNotifyDaoImpl.java
public Page<ProductNotify> findPage(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent, Pageable pageable) {//from ww w .ja v a 2 s. co m CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class); Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (isMarketable != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product").get("goods").get("isMarketable"), isMarketable)); } if (isOutOfStock != null) { Path<Integer> stock = root.get("product").get("stock"); Path<Integer> allocatedStock = root.get("product").get("allocatedStock"); if (isOutOfStock) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock), criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock)); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock), criteriaBuilder.greaterThan(stock, allocatedStock))); } } if (hasSent != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent)); } criteriaQuery.where(restrictions); return super.findPage(criteriaQuery, pageable); }
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 www.j av a2 s . co 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:com.yunguchang.data.ApplicationRepository.java
public List<TBusApplyinfoEntity> getApplicationForSync(DateTime startTime, DateTime endTime, String status, Boolean isSend, PrincipalExt principalExt) { if (startTime == null) { startTime = DateTimeUtil.appStartTime; }/*from w ww. j a va2s .c o m*/ if (endTime == null) { endTime = DateTimeUtil.appEndTime; } CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<TBusApplyinfoEntity> cq = cb.createQuery(TBusApplyinfoEntity.class); Root<TBusApplyinfoEntity> applyRoot = cq.from(TBusApplyinfoEntity.class); applyRoot.fetch(TBusApplyinfoEntity_.passenger); applyRoot.fetch(TBusApplyinfoEntity_.coordinator); applyRoot.fetch(TBusApplyinfoEntity_.department); applyRoot.fetch(TBusApplyinfoEntity_.senduser, JoinType.LEFT); Predicate predicate = cb.conjunction(); predicate = cb.and(predicate, cb.or(cb.isNull(applyRoot.get(TBusApplyinfoEntity_.updateBySync)), cb.isFalse(applyRoot.get(TBusApplyinfoEntity_.updateBySync)))); if (status != null) { predicate = cb.and(predicate, // "2" -> "3" -> ? "4" -> ? cb.equal(applyRoot.get(TBusApplyinfoEntity_.status), status)); } if (isSend != null && isSend) { predicate = cb.and(predicate, cb.equal(applyRoot.get(TBusApplyinfoEntity_.issend), "1") // ); } if ("4".equals(status)) { Fetch<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleFetch = applyRoot .fetch(TBusApplyinfoEntity_.schedule, JoinType.LEFT); Join<TBusApplyinfoEntity, TBusScheduleRelaEntity> scheduleJoin = (Join<TBusApplyinfoEntity, TBusScheduleRelaEntity>) scheduleFetch; scheduleJoin.fetch(TBusScheduleRelaEntity_.reciveuser, JoinType.LEFT); Fetch<TBusScheduleRelaEntity, TBusScheduleCarEntity> fetchScheduleCar = scheduleFetch .fetch(TBusScheduleRelaEntity_.scheduleCars, JoinType.LEFT); fetchScheduleCar.fetch(TBusScheduleCarEntity_.car, JoinType.LEFT).fetch(TAzCarinfoEntity_.depot, JoinType.LEFT); fetchScheduleCar.fetch(TBusScheduleCarEntity_.driver, JoinType.LEFT) .fetch(TRsDriverinfoEntity_.department, JoinType.LEFT); predicate = cb.and(predicate, cb.or(cb.between(applyRoot.get(TBusApplyinfoEntity_.updatedate), startTime, endTime), cb.between(scheduleJoin.get(TBusScheduleRelaEntity_.updatedate), startTime, endTime))); } else { predicate = cb.and(predicate, cb.between(applyRoot.get(TBusApplyinfoEntity_.updatedate), startTime, endTime)); } cq.where(predicate); TypedQuery<TBusApplyinfoEntity> query = em.createQuery(cq); // applySecurityFilter("applications", principalExt); return query.getResultList(); }
From source file:com.yunguchang.data.ApplicationRepository.java
public List<TBusApproveSugEntity> listApplyApproveInfo(DateTime startTime, DateTime endTime, PrincipalExt principalExtOrNull) { if (startTime == null) { startTime = DateTimeUtil.appStartTime; }// www.j a va 2s. co m if (endTime == null) { endTime = DateTimeUtil.appEndTime; } CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<TBusApproveSugEntity> cq = cb.createQuery(TBusApproveSugEntity.class); Root<TBusApproveSugEntity> approveRoot = cq.from(TBusApproveSugEntity.class); approveRoot.fetch(TBusApproveSugEntity_.application); approveRoot.fetch(TBusApproveSugEntity_.user, JoinType.LEFT); cq.select(approveRoot); Predicate predicate = cb.and( cb.between(approveRoot.get(TBusApproveSugEntity_.operatedate), new Timestamp(startTime.getMillis()), new Timestamp(endTime.getMillis())), cb.or(cb.isNull(approveRoot.get(TBusApproveSugEntity_.updateBySync)), cb.isFalse(approveRoot.get(TBusApproveSugEntity_.updateBySync)))); cq.where(predicate); TypedQuery<TBusApproveSugEntity> query = em.createQuery(cq); applySecurityFilter("applications", principalExtOrNull); return query.getResultList(); }
From source file:aode.lx.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 .com public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (Collections3.isNotEmpty(filters)) { List<Predicate> predicates = Lists.newArrayList(); List<Predicate> ORpredicates = 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]); } Object value = filter.value; if (expression.getJavaType().isEnum()) { value = EnumUtils.valueOf(expression.getJavaType(), filter.value.toString()); predicates.add(builder.equal(expression, value)); } else { // 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 NOTNULL: predicates.add(builder.isNotNull(expression)); break; case ISNULL: predicates.add(builder.isNull(expression)); break; case OREQ: ORpredicates.add(builder.equal(expression, filter.value)); break; case ORLIKE: ORpredicates.add(builder.like(expression, "%" + filter.value + "%")); break; case ORGT: ORpredicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case ORLT: ORpredicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case ORGTE: ORpredicates .add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case ORLTE: ORpredicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; case ORNEQ: ORpredicates.add(builder.notEqual(expression, filter.value)); break; case ORNOTNULL: ORpredicates.add(builder.isNotNull(expression)); break; case ORISNULL: ORpredicates.add(builder.isNull(expression)); break; } } } // ? and ??? if (!predicates.isEmpty() && !ORpredicates.isEmpty()) { return builder.and(builder.and(predicates.toArray(new Predicate[predicates.size()])), builder.or(ORpredicates.toArray(new Predicate[ORpredicates.size()]))); } if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } if (!ORpredicates.isEmpty()) { return builder.or(ORpredicates.toArray(new Predicate[ORpredicates.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/*from w ww.j a v a 2 s . c o 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(); } }; }