@Override public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) { return explicitPath.in(directValues); } }
@Override public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path<Serializable> explicitPath, List<Serializable> directValues) { Path<Serializable> path; if (explicitPath != null) { path = explicitPath; } else { path = fieldPathBuilder.getPath(root, fullPropertyName, builder); } return path.in(directValues); } }
@SuppressWarnings("unchecked") protected Predicate buildSkuDomainPredicate(CriteriaBuilder cb, Path fieldName, List<Long> possibleSkuIds) { int listSize = possibleSkuIds.size(); Predicate predicate = null; for (int i = 0; i < listSize; i += IN_CLAUSE_LIMIT) { List subList; if (listSize > i + IN_CLAUSE_LIMIT) { subList = possibleSkuIds.subList(i, (i + IN_CLAUSE_LIMIT)); } else { subList = possibleSkuIds.subList(i, listSize); } if (predicate == null) { predicate = fieldName.in(subList); } else { predicate = cb.or(predicate, fieldName.in(subList)); } } return predicate; }
@Override public Long countAllowedValuesForProductOptionById(Long productOptionId) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = builder.createQuery(Long.class); Root<ProductOptionValueImpl> root = criteria.from(ProductOptionValueImpl.class); criteria.select(builder.count(root)); List<Predicate> restrictions = new ArrayList<>(); List<Long> mergedIds = sandBoxHelper.mergeCloneIds(ProductOptionImpl.class, productOptionId); restrictions.add(root.get("productOption").in(mergedIds)); criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); TypedQuery<Long> query = em.createQuery(criteria); return query.getSingleResult(); }
protected <T> void applyLimitedInClause(List<Long> ids, CriteriaBuilder builder, Root<T> root, List<Predicate> restrictions) { List<List<Long>> listsOfExcludeIds = Lists.partition(ids, RESTRICT_IN_CLAUSE_MAX_SIZE); List<Predicate> inRestrictions = new ArrayList<Predicate>(); for (List<Long> idSetToExclude : listsOfExcludeIds) { inRestrictions.add(builder.not(root.get("id").in(idSetToExclude))); } restrictions.add(builder.and(inRestrictions.toArray(new Predicate[inRestrictions.size()]))); }
@Override public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Path<?> path = root.get(entityInformation.getIdAttribute()); parameter = cb.parameter(Iterable.class); return path.in(parameter); } }
@Override public List<Order> readOrdersForCustomersInDateRange(List<Long> customerIds, Date startDate, Date endDate) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Order> criteria = builder.createQuery(Order.class); Root<OrderImpl> order = criteria.from(OrderImpl.class); criteria.select(order); List<Predicate> restrictions = new ArrayList<>(); restrictions.add(builder.between(order.<Date>get("submitDate"), startDate, endDate)); restrictions.add(order.get("customer").get("id").in(customerIds)); criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); criteria.orderBy(builder.desc(order.get("customer")), builder.asc(order.get("submitDate"))); TypedQuery<Order> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Order"); return query.getResultList(); }
public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) { return root.get("productOption").get("id").in(directValues); } }));
@Override public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) { return root.get("productOption").get("id").in(directValues); } }));
@Override public Predicate buildPredicate(CriteriaBuilder builder, FieldPathBuilder fieldPathBuilder, From root, String ceilingEntity, String fullPropertyName, Path explicitPath, List directValues) { Path<Serializable> path; if (explicitPath != null) { path = explicitPath; } else { path = fieldPathBuilder.getPath(root, fullPropertyName, builder); } return path.in(directValues).not(); }
@Override @Transactional(value="defaultTransactionManager") public void clearDuplicateRefreshTokens() { Query query = manager.createQuery("select a.jwt, count(1) as c from OAuth2RefreshTokenEntity a GROUP BY a.jwt HAVING count(1) > 1"); @SuppressWarnings("unchecked") List<Object[]> resultList = query.getResultList(); List<JWT> values = new ArrayList<>(); for (Object[] r : resultList) { logger.warn("Found duplicate refresh tokens: {}, {}", ((JWT)r[0]).serialize(), r[1]); values.add((JWT) r[0]); } if (values.size() > 0) { CriteriaBuilder cb = manager.getCriteriaBuilder(); CriteriaDelete<OAuth2RefreshTokenEntity> criteriaDelete = cb.createCriteriaDelete(OAuth2RefreshTokenEntity.class); Root<OAuth2RefreshTokenEntity> root = criteriaDelete.from(OAuth2RefreshTokenEntity.class); criteriaDelete.where(root.get("jwt").in(values)); int result = manager.createQuery(criteriaDelete).executeUpdate(); logger.warn("Deleted {} duplicate refresh tokens", result); } }
@Override @Transactional(value="defaultTransactionManager") public void clearDuplicateAccessTokens() { Query query = manager.createQuery("select a.jwt, count(1) as c from OAuth2AccessTokenEntity a GROUP BY a.jwt HAVING count(1) > 1"); @SuppressWarnings("unchecked") List<Object[]> resultList = query.getResultList(); List<JWT> values = new ArrayList<>(); for (Object[] r : resultList) { logger.warn("Found duplicate access tokens: {}, {}", ((JWT)r[0]).serialize(), r[1]); values.add((JWT) r[0]); } if (values.size() > 0) { CriteriaBuilder cb = manager.getCriteriaBuilder(); CriteriaDelete<OAuth2AccessTokenEntity> criteriaDelete = cb.createCriteriaDelete(OAuth2AccessTokenEntity.class); Root<OAuth2AccessTokenEntity> root = criteriaDelete.from(OAuth2AccessTokenEntity.class); criteriaDelete.where(root.get("jwt").in(values)); int result = manager.createQuery(criteriaDelete).executeUpdate(); logger.warn("Deleted {} duplicate access tokens", result); } }
@Override public List<OrderItem> readOrderItemsForCustomersInDateRange(List<Long> customerIds, Date startDate, Date endDate) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<OrderItem> criteria = builder.createQuery(OrderItem.class); Root<OrderImpl> order = criteria.from(OrderImpl.class); Join<Order, OrderItem> orderItems = order.join("orderItems"); criteria.select(orderItems); List<Predicate> restrictions = new ArrayList<>(); restrictions.add(builder.between(order.<Date>get("submitDate"), startDate, endDate)); restrictions.add(order.get("customer").get("id").in(customerIds)); criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); criteria.orderBy(builder.desc(order.get("customer")), builder.asc(order.get("submitDate"))); TypedQuery<OrderItem> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Order"); return query.getResultList(); }
private TypedQuery<Long> getProductIdsUsingProductOptionByIdQuery(Long productOptionId, boolean count) { // Set up the criteria query that specifies we want to return Products CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = builder.createQuery(Long.class); // The root of our search is ProductOptionXref Root<ProductOptionXrefImpl> productOptionXref = criteria.from(ProductOptionXrefImpl.class); Join<ProductOptionXref, Product> product = productOptionXref.join("product"); Join<ProductOptionXref, ProductOption> productOption = productOptionXref.join("productOption"); if (count) { criteria.select(builder.count(product)); } else { // Product IDs are what we want back criteria.select(product.get("id").as(Long.class)); } criteria.distinct(true); List<Predicate> restrictions = new ArrayList<Predicate>(); restrictions.add(productOption.get("id").in(sandBoxHelper.mergeCloneIds(ProductOptionImpl.class, productOptionId))); // Execute the query with the restrictions criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); return em.createQuery(criteria); }
public List<Translation> readAllTranslationEntries(TranslatedEntity entityType, ResultType stage, List<String> entityIds) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class); Root<TranslationImpl> root = criteria.from(TranslationImpl.class); criteria.select(root); List<Predicate> restrictions = new ArrayList<Predicate>(); restrictions.add(builder.equal(root.get("entityType"), entityType.getFriendlyType())); if (CollectionUtils.isNotEmpty(entityIds)) { restrictions.add(root.get("entityId").in(entityIds)); } try { if (extensionManager != null) { extensionManager.getProxy().setup(TranslationImpl.class, stage); extensionManager.getProxy().refineParameterRetrieve(TranslationImpl.class, stage, builder, criteria, root, restrictions); } criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); TypedQuery<Translation> query = em.createQuery(criteria); if (extensionManager != null) { extensionManager.getProxy().refineQuery(TranslationImpl.class, stage, query); } query.setHint(QueryHints.HINT_CACHEABLE, true); return query.getResultList(); } finally { if (extensionManager != null) { extensionManager.getProxy().breakdown(TranslationImpl.class, stage); } } }
restrictions.add(category.get("id").in(sandBoxHelper.mergeCloneIds(CategoryImpl.class, categoryId)));
predicates.add(root.get("sku").get("product").get("id").in(sandBoxHelper.mergeCloneIds(ProductImpl.class, productId)));
@Override public List<SandBox> retrieveChildSandBoxesByParentId(Long parentSandBoxId) { CriteriaBuilder builder = sandBoxEntityManager.getCriteriaBuilder(); CriteriaQuery<SandBox> criteria = builder.createQuery(SandBox.class); Root<SandBoxManagementImpl> sandbox = criteria.from(SandBoxManagementImpl.class); criteria.select(sandbox.get("sandBox").as(SandBox.class)); criteria.where( builder.and(sandbox.get("sandBox").get("parentSandBox").in(parentSandBoxId), builder.or(builder.isNotNull(sandbox.get("sandBox").get("name")), builder.notEqual(sandbox.get("sandBox").get("name").as(String.class), "")), builder.or(builder.isNull(sandbox.get("sandBox").get("archiveStatus").get("archived").as(String.class)), builder.notEqual(sandbox.get("sandBox").get("archiveStatus").get("archived").as(Character.class), 'Y'))) ); TypedQuery<SandBox> query = sandBoxEntityManager.createQuery(criteria); return query.getResultList(); }
public <R> Predicate createPredicate(QueryCriteria criteria, CriteriaQuery<R> query, CriteriaBuilder builder) { // subquery and root Root<TaskImpl> taskRoot = getRoot(query, TaskImpl.class); Subquery<Long> subQuery = query.subquery(Long.class); Root<TaskVariableImpl> taskVarRoot = subQuery.from(TaskVariableImpl.class); subQuery.select(taskVarRoot.get(TaskVariableImpl_.taskId)); // task variable predicate (in subquery) Predicate taskVariablePredicate = null; String listId = criteria.getListId(); if( TASK_VARIABLE_COMBINED_ID.equals(listId) ) { List<QueryCriteria> taskVarSubCriteriaList = criteria.getCriteria(); int size = taskVarSubCriteriaList.size(); Predicate[] taskVarSubPredicates = new Predicate[size]; for( int i = 0; i < size; ++i ) { taskVarSubPredicates[i] = createSingleTaskVariableCriteriaPredicate( builder, taskVarRoot, taskVarSubCriteriaList.get(i)); } taskVariablePredicate = builder.and(taskVarSubPredicates); } else { taskVariablePredicate = createSingleTaskVariableCriteriaPredicate(builder, taskVarRoot, criteria); } // add predicate to subquery subQuery.where(taskVariablePredicate); // create predicate for actual query that references subquery return taskRoot.get(TaskImpl_.id).in(subQuery); }
@Test public void testInClauseParameterPadding() { sqlStatementInterceptor.clear(); doInJPA( this::entityManagerFactory, entityManager -> { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = cb.createQuery( Long.class ); Root<Document> document = query.from( Document.class ); ParameterExpression<List> inClauseParams = cb.parameter( List.class, "ids" ); query .select( document.get( "id" ) ) .where( document.get( "id" ).in( inClauseParams ) ); List<Long> ids = entityManager.createQuery( query ) .setParameter( "ids", Arrays.asList( 1, 2, 3, 4, 5 ) ) .getResultList(); assertEquals( 1, ids.size() ); } ); assertTrue( sqlStatementInterceptor.getSqlQueries().get( 0 ).endsWith( "in (? , ? , ? , ? , ? , ? , ? , ?)" ) ); }