@Override public Expression<?> joinMapValue(Expression<?> currentCriteriaPath, MetaAttribute pathElement, Object key) { MapJoin<Object, Object, Object> mapJoin = ((From<?, ?>) currentCriteriaPath).joinMap(pathElement.getName(), JoinType.LEFT); Predicate mapJoinCondition = cb.equal(mapJoin.key(), key); Predicate nullCondition = cb.isNull(mapJoin.key()); addPredicate(cb.or(mapJoinCondition, nullCondition)); return mapJoin; }
@Override public int sumProductNum(Product product, LocalDateTime beginTime, LocalDateTime endTime, OrderStatus... orderStatuses) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); Root<MainOrder> root = cq.from(MainOrder.class); //今日核算时间之前的订单 MapJoin<MainOrder, MainGood, Integer> amountsRoot = root.join(MainOrder_.amounts); List<Predicate> list = new ArrayList<>(); list.add(cb.notEqual(root.get(MainOrder_.orderStatus), OrderStatus.close)); list.add(cb.equal(amountsRoot.key().get(MainGood_.product), product)); if (orderStatuses != null) { list.add(root.get(MainOrder_.orderStatus) .in(orderStatuses)); } if (beginTime != null) { list.add(cb.greaterThanOrEqualTo(root.get(MainOrder_.orderTime), beginTime)); } if (endTime != null) { list.add(cb.lessThanOrEqualTo(root.get(MainOrder_.orderTime), endTime)); } Predicate[] p = new Predicate[list.size()]; cq.where(cb.and(list.toArray(p))); cq.select(cb.sum(amountsRoot.value())); Object result = entityManager.createQuery(cq).getSingleResult(); return result != null ? (int) result : 0; }
public static Predicate<BuildRecord> withAttribute(String key, String value) { return (root, query, cb) -> { MapJoin<Object, Object, Object> mapJoinAttributes = root.joinMap(BuildRecord_.attributes.getName()); return query.where(cb.and(cb.equal(mapJoinAttributes.key(), key), cb.equal(mapJoinAttributes.value(), value))).getRestriction(); }; }
@SuppressWarnings("unchecked") private Predicate mapToMapPredicate(final ComparisonNode node, final Path<Object> fieldPath, final A enumField) { if (!enumField.isMap()) { return null; } final String[] graph = node.getSelector().split("\\" + FieldNameProvider.SUB_ATTRIBUTE_SEPERATOR); final String keyValue = graph[graph.length - 1]; if (fieldPath instanceof MapJoin) { // Currently we support only string key .So below cast is safe. return cb.equal(cb.upper((Expression<String>) (((MapJoin<?, ?, ?>) fieldPath).key())), keyValue.toUpperCase()); } final String keyFieldName = enumField.getSubEntityMapTuple().map(Entry::getKey) .orElseThrow(() -> new UnsupportedOperationException( "For the fields, defined as Map, only Map java type or tuple in the form of SimpleImmutableEntry are allowed. Neither of those could be found!")); return cb.equal(cb.upper(fieldPath.get(keyFieldName)), keyValue.toUpperCase()); }
@SuppressWarnings("unchecked") private Predicate mapToMapPredicate(final ComparisonNode node, final Path<Object> fieldPath, final A enumField) { if (!enumField.isMap()) { return null; } final String[] graph = node.getSelector().split("\\" + FieldNameProvider.SUB_ATTRIBUTE_SEPERATOR); final String keyValue = graph[graph.length - 1]; if (fieldPath instanceof MapJoin) { // Currently we support only string key .So below cast is safe. return cb.equal(cb.upper((Expression<String>) (((MapJoin<?, ?, ?>) fieldPath).key())), keyValue.toUpperCase()); } final String keyFieldName = enumField.getSubEntityMapTuple().map(Entry::getKey) .orElseThrow(() -> new UnsupportedOperationException( "For the fields, defined as Map, only Map java type or tuple in the form of SimpleImmutableEntry are allowed. Neither of those could be found!")); return cb.equal(cb.upper(fieldPath.get(keyFieldName)), keyValue.toUpperCase()); }
predicateList.add(cb.equal(root.join(MainGood_.product).joinMap(Product_.propertyNameValues.getName()).key().get(PropertyName_.id.getName()), property)); predicateList.add(cb.equal(root.join(MainGood_.product).joinMap(Product_.propertyNameValues.getName()).value(), propertyNameValueMap.get(property))); });
predicateList.add(cb.equal(root.join(MainGood_.product).joinMap(Product_.propertyNameValues.getName()).key().get(PropertyName_.id.getName()), property)); predicateList.add(cb.equal(root.join(MainGood_.product).joinMap(Product_.propertyNameValues.getName()).value(), propertyValueMap.get(property))); });
predicates.add(root.join("scopes").get("id").in(value)); } else if (name.startsWith("config:")) { predicates.add(root.joinMap("config").key().in(name.substring("config:".length()))); predicates.add(builder.like(root.joinMap("config").value().as(String.class), "%" + value[0] + "%")); } else {
amountJoin = amountJoin.on(cb.equal(amountJoin.key(), product));
predicate = cb.and(predicate, cb.equal(root.join(MainDeliverableOrder_.amounts).key().get(MainGood_.id), goodId));
@Override public Specification<MainOrder> specification() { return (root, query, cb) -> { Predicate predicate = cb.equal(unitJoin.type(), HaierOrder.class); if (!StringUtils.isEmpty(mobile)) predicate = cb.and(predicate, cb.like(Customer.getMobile(MainOrder.getCustomer(root)) , "%" + mobile + "%")); if (depotId != null) { predicate = cb.and(predicate, cb.equal(unitJoin.get("origin").get("id"), depotId)); } if (!StringUtils.isEmpty(productCode)) { root.fetch(MainOrder_.amounts); predicate = cb.and(predicate , cb.equal(root.join(MainOrder_.amounts).key().get(MainGood_.product).get(Product_.code) , productCode)); } if (orderDate != null) { predicate = cb.and(predicate, JpaFunctionUtils.dateEqual(cb, root.get("orderTime"), orderDate)); } return predicate; }; } };
customPropertyPredicates = new ArrayList<>(); customPropertyPredicates.add(criteriaBuilder.equal(customPropertyFrom.get("id"), relationshipFrom.get("id"))); customPropertyPredicates.add(criteriaBuilder.equal(customPropertyJoin.key(), otherAttributeKey)); customPropertyValuePath = customPropertyJoin.value(); predicates.add(criteriaBuilder.exists(customPropertySubquery)); customPropertyPredicates = new ArrayList<>(); customPropertyPredicates.add(criteriaBuilder.equal(customPropertyFrom.get("id"), targetFrom.get("id"))); customPropertyPredicates.add(criteriaBuilder.equal(customPropertyJoin.key(), otherAttributeKey)); customPropertyValuePath = customPropertyJoin.value(); predicates.add(criteriaBuilder.exists(customPropertySubquery));
@Override public Specification<StockShiftUnit> specification() { return (root, query, cb) -> { final Join<StockShiftUnit, Depot> stockShiftUnitDepotJoin = StockShiftUnit.destinationJoin(root); Predicate predicate = cb.and( stockShiftUnitDepotJoin.isNotNull(), StockShiftUnit.originJoin(root).isNull() ); if (!StringUtils.isEmpty(mobile)) predicate = cb.and(predicate, cb.like(Depot.mobile(stockShiftUnitDepotJoin) , "%" + mobile + "%")); if (depotId != null) predicate = cb.and(predicate, cb.equal(stockShiftUnitDepotJoin.get("id"), depotId)); // productCode 包含这个商品 if (!StringUtils.isEmpty(productCode)) { MapJoin<StockShiftUnit, Product, ProductBatch> amountJoin = root.joinMap("amounts"); predicate = cb.and(predicate, cb.equal(amountJoin.key().get("code"), productCode)); } if (orderDate != null) { predicate = cb.and( predicate , JpaFunctionUtils.dateEqual(cb, StockShiftUnit.createDate(root), orderDate)); } return predicate; }; } };