@Override public Boolean offerCodeIsUsed(OfferCode code) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Order> criteria = builder.createQuery(Order.class); Root<OrderImpl> baseOrder = criteria.from(OrderImpl.class); criteria.select(baseOrder); Join<OrderImpl, OfferCodeImpl> join = baseOrder.join("addedOfferCodes"); criteria.where(builder.equal(join.get("id"), code.getId())); TypedQuery<Order> query = em.createQuery(criteria); try { query.getSingleResult(); } catch (NoResultException e) { return false; } return true; }
@Override public List<IndexField> readAllIndexFieldsByFieldId(Long fieldId) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<IndexField> criteria = builder.createQuery(IndexField.class); Root<IndexFieldImpl> search = criteria.from(IndexFieldImpl.class); criteria.select(search); criteria.where( builder.equal(search.join("field").get("id").as(Long.class), fieldId) ); TypedQuery<IndexField> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Search"); return query.getResultList(); }
protected Long readCountAllActiveProductsInternal(Date currentDate) { // Set up the criteria query that specifies we want to return a Long CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = builder.createQuery(Long.class); // The root of our search is Product Root<ProductImpl> product = criteria.from(ProductImpl.class); // We need to filter on active date on the sku Join<Product, Sku> sku = product.join("defaultSku"); // We want the count of products criteria.select(builder.count(product)); // Ensure the product is currently active List<Predicate> restrictions = new ArrayList<Predicate>(); attachActiveRestriction(currentDate, product, sku, restrictions); // Add the restrictions to the criteria query criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); TypedQuery<Long> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog"); return query.getSingleResult(); }
@Override public IndexField readIndexFieldByFieldId(Long fieldId) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<IndexField> criteria = builder.createQuery(IndexField.class); Root<IndexFieldImpl> search = criteria.from(IndexFieldImpl.class); criteria.select(search); criteria.where( builder.equal(search.join("field").get("id").as(Long.class), fieldId) ); TypedQuery<IndexField> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Search"); try { return query.getSingleResult(); } catch (NoResultException e) { return null; } }
@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); }
@Override public List<OrderItem> readBatchOrderItems(int start, int count, List<OrderStatus> statuses) { 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<>(); criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); if (CollectionUtils.isNotEmpty(statuses)) { // We only want results that match the orders with the correct status ArrayList<String> statusStrings = new ArrayList<String>(); for (OrderStatus status : statuses) { statusStrings.add(status.getType()); } criteria.where(order.get("status").as(String.class).in(statusStrings)); } TypedQuery<OrderItem> query = em.createQuery(criteria); query.setFirstResult(start); query.setMaxResults(count); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Order"); return query.getResultList(); } }
protected CriteriaQuery<Product> getCriteriaForActiveProducts(Date currentDate, Long lastId) { // Set up the criteria query that specifies we want to return Products CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = builder.createQuery(Product.class); // The root of our search is Product Root<ProductImpl> product = criteria.from(ProductImpl.class); // We need to filter on active date on the sku Join<Product, Sku> sku = product.join("defaultSku"); product.fetch("defaultSku"); // Product objects are what we want back criteria.select(product); // Ensure the product is currently active List<Predicate> restrictions = new ArrayList<Predicate>(); attachActiveRestriction(currentDate, product, sku, restrictions); if (lastId != null) { restrictions.add(builder.gt(product.get("id").as(Long.class), lastId)); } // Add the restrictions to the criteria query criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); //Add ordering so that paginated queries are consistent criteria.orderBy(builder.asc(product.get("id"))); return criteria; }
@Override public SearchFacet readSearchFacetForField(Field field) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<SearchFacet> criteria = builder.createQuery(SearchFacet.class); Root<SearchFacetImpl> facet = criteria.from(SearchFacetImpl.class); criteria.select(facet); criteria.where( builder.equal(facet.join("fieldType").join("indexField").join("field").get("id").as(Long.class), field.getId()) ); TypedQuery<SearchFacet> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Search"); try { return query.getSingleResult(); } catch (NoResultException e) { return null; } }
fieldName = fieldName.substring("defaultSku.".length()); } else if (fieldName.contains("productAttributes.")) { pathToUse = product.join("productAttributes");
|| listId.equals(CREATED_BY_LIST) ) { if( taskDataJoin == null ) { taskDataJoin = taskRoot.join(TaskImpl_.taskData); || listId.equals(EXCLUDED_OWNER_ID_LIST) ) { if( peopleAssignJoin == null ) { peopleAssignJoin = taskRoot.join(TaskImpl_.peopleAssignments); } else { if( taskDataJoin == null ) { taskDataJoin = taskRoot.join(TaskImpl_.taskData);
Join<Product, Sku> sku = product.join("defaultSku");
@Test public void test_criteria_from_join_example() { doInJPA( this::entityManagerFactory, entityManager -> { //tag::criteria-from-join-example[] CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Phone> criteria = builder.createQuery( Phone.class ); Root<Phone> root = criteria.from( Phone.class ); // Phone.person is a @ManyToOne Join<Phone, Person> personJoin = root.join( Phone_.person ); // Person.addresses is an @ElementCollection Join<Person, String> addressesJoin = personJoin.join( Person_.addresses ); criteria.where( builder.isNotEmpty( root.get( Phone_.calls ) ) ); List<Phone> phones = entityManager.createQuery( criteria ).getResultList(); //end::criteria-from-join-example[] assertEquals(2, phones.size()); }); }
@Override public List<SearchFacet> readAllSearchFacets(FieldEntity entityType) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<SearchFacet> criteria = builder.createQuery(SearchFacet.class); Root<SearchFacetImpl> facet = criteria.from(SearchFacetImpl.class); criteria.select(facet); Path<Character> archived = facet.get("archiveStatus").get("archived"); criteria.where( builder.equal(facet.get("showOnSearch").as(Boolean.class), true), builder.or(builder.isNull(archived.as(String.class)), builder.notEqual(archived.as(Character.class), 'Y')), facet.join("fieldType") .join("indexField") .join("field") .get("entityType") .as(String.class) .in(entityType.getAllLookupTypes()) ); TypedQuery<SearchFacet> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Search"); return query.getResultList(); }
@Override protected <T> Expression getEntityField(CriteriaQuery<T> query, String listId, Attribute attr) { if( attr == null ) { return null; } Root<TaskImpl> taskRoot = null; Join<TaskImpl, TaskDataImpl> taskDataJoin = null; Join<TaskImpl, PeopleAssignmentsImpl> peopAssignJoin = null; for( Root root : query.getRoots() ) { if( TaskImpl.class.equals(root.getJavaType()) ) { taskRoot = (Root<TaskImpl>) root; for( Join<TaskImpl, ?> join : taskRoot.getJoins() ) { if( TaskDataImpl.class.equals(join.getJavaType()) ) { taskDataJoin = (Join<TaskImpl, TaskDataImpl>) join; } else if( PeopleAssignmentsImpl.class.equals(join.getJavaType()) ) { peopAssignJoin = (Join<TaskImpl, PeopleAssignmentsImpl>) join; } } } } assert taskRoot != null : "Unable to find TaskImpl Root in query!"; if( taskDataJoin == null ) { taskDataJoin = taskRoot.join(TaskImpl_.taskData); } assert taskDataJoin != null : "Unable to find TaskDataImpl Join in query!"; return taskImplSpecificGetEntityField(query, taskRoot, taskDataJoin, peopAssignJoin, listId, attr); }
peopleAssignJoin = taskRoot.join(TaskImpl_.peopleAssignments);
taskRoot.join(TaskImpl_.taskData); // added for convienence sake, since other logic expects to find this join
Join<TaskImpl, TaskDataImpl> join = taskRoot.join(TaskImpl_.taskData); Join<TaskImpl, PeopleAssignmentsImpl> peopleAssign = taskRoot.join(TaskImpl_.peopleAssignments); ListJoin<PeopleAssignmentsImpl,OrganizationalEntityImpl> busAdmins = peopleAssign.join(PeopleAssignmentsImpl_.businessAdministrators, JoinType.LEFT); ListJoin<PeopleAssignmentsImpl,OrganizationalEntityImpl> potOwners = peopleAssign.join(PeopleAssignmentsImpl_.potentialOwners, JoinType.LEFT);
switch (theType) { case DATE: join = myResourceTableRoot.join("myParamsDate", JoinType.LEFT); break; case NUMBER: join = myResourceTableRoot.join("myParamsNumber", JoinType.LEFT); break; case QUANTITY: join = myResourceTableRoot.join("myParamsQuantity", JoinType.LEFT); break; case REFERENCE: join = myResourceTableRoot.join("myResourceLinks", JoinType.LEFT); break; case STRING: join = myResourceTableRoot.join("myParamsString", JoinType.LEFT); break; case URI: join = myResourceTableRoot.join("myParamsUri", JoinType.LEFT); break; case TOKEN: join = myResourceTableRoot.join("myParamsToken", JoinType.LEFT); break;