@SuppressWarnings("unchecked") public <E, F> Path<F> getPath(Root<E> root, List<Attribute<?, ?>> attributes) { Path<?> path = root; for (Attribute<?, ?> attribute : attributes) { boolean found = false; if (path instanceof FetchParent) { for (Fetch<E, ?> fetch : ((FetchParent<?, E>) path).getFetches()) { if (attribute.getName().equals(fetch.getAttribute().getName()) && (fetch instanceof Join<?, ?>)) { path = (Join<E, ?>) fetch; found = true; break; } } } if (!found) { if ((attributes.indexOf(attribute) != (attributes.size() - 1)) && (attribute instanceof Bindable) && Identifiable.class.isAssignableFrom(((Bindable<?>) attribute).getBindableJavaType()) && (path instanceof From)) { path = ((From<?, ?>) path).join(attribute.getName(), JoinType.LEFT); } else { path = path.get(attribute.getName()); } } } return (Path<F>) path; }
@SuppressWarnings({ "unchecked", "rawtypes" }) public <E> void fetches(SearchParameters<E> sp, Root<E> root) { for (jpasearch.repository.query.Path<E, ?> path : sp.getFetches()) { FetchParent<?, ?> from = root; for (Attribute<?, ?> arg : metamodelUtil.toAttributes(root.getJavaType(), path.getPath())) { boolean found = false; for (Fetch<?, ?> fetch : from.getFetches()) { if (arg.equals(fetch.getAttribute())) { from = fetch; found = true; break; } } if (!found) { if (arg instanceof PluralAttribute) { from = from.fetch((PluralAttribute) arg, JoinType.LEFT); } else { from = from.fetch((SingularAttribute) arg, JoinType.LEFT); } } } } }
/** * Fetch association. */ private <T> void fetchAssociation(final Root<T> root, final Map.Entry<String, JoinType> entry, final JoinType joinType) { final String[] propertiesToFetch = StringUtils.split(entry.getKey(), '.'); FetchParent<?, ?> previouslyFetched = root; for (final String property : propertiesToFetch) { previouslyFetched = getFetchedAssoc(previouslyFetched, joinType, property); } }
private Fetch<?, ?> getFetchedAssoc(final FetchParent<?, ?> parent, final JoinType joinType, final String propertyToFetch) { // Search within current fetches for (final Fetch<?, ?> fetch : parent.getFetches()) { if (fetch.getAttribute().getName().equals(propertyToFetch)) { return fetch; } } // Create a new one return parent.fetch(propertyToFetch, joinType); }
@Override public List<Product> readProductsByIds(List<Long> productIds) { if (productIds == null || productIds.size() == 0) { return null; } if (productIds.size() > 100) { logger.warn("Not recommended to use the readProductsByIds method for long lists of productIds, since " + "Hibernate is required to transform the distinct results. The list of requested" + "product ids was (" + productIds.size() +") in length."); } // Set up the criteria query that specifies we want to return Products CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = builder.createQuery(Product.class); Root<ProductImpl> product = criteria.from(ProductImpl.class); FetchParent fetchParent = product.fetch("defaultSku", JoinType.LEFT); if (!dialectHelper.isOracle() && !dialectHelper.isSqlServer()) { fetchParent.fetch("skuMedia", JoinType.LEFT); } criteria.select(product); // We only want results that match the product IDs criteria.where(product.get("id").as(Long.class).in( sandBoxHelper.mergeCloneIds(em, ProductImpl.class, productIds.toArray(new Long[productIds.size()])))); if (!dialectHelper.isOracle() && !dialectHelper.isSqlServer()) { criteria.distinct(true); } TypedQuery<Product> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog"); return query.getResultList(); }
private void bindFetches(FetchParent<?, ?> lhs, SqmNavigableReference lhsBinding, SqmFromElementSpace space) { if ( !SqmNavigableSourceReference.class.isInstance( lhsBinding ) ) { if ( !lhs.getFetches().isEmpty() ) { throw new ParsingException( "Attempt to bind fetches against a NavigableBinding that is not also a NavigableSourceBinding " ); } else { return; } } final SqmNavigableSourceReference sourceBinding = (SqmNavigableSourceReference) lhsBinding; for ( Fetch<?, ?> fetch : lhs.getFetches() ) { final JpaFetch<?,?> jpaFetch = (JpaFetch<?, ?>) fetch; final SqmAttributeReference attrBinding = (SqmAttributeReference) parsingContext.findOrCreateNavigableBinding( sourceBinding, fetch.getAttribute().getName() ); // todo : handle treats final SqmAttributeJoin sqmFetch = querySpecProcessingStateStack.getCurrent().getFromElementBuilder().buildAttributeJoin( attrBinding, interpretAlias( jpaFetch.getAlias() ), // todo : this is where treat would be applied null, convert( fetch.getJoinType() ), true, false ); space.addJoin( sqmFetch ); bindFetches( fetch, sqmFetch.getBinding(), space ); jpaPathResolutionMap.put( jpaFetch, sqmFetch.getBinding() ); } }
@Override public List<Product> readProductsByIds(List<Long> productIds) { if (productIds == null || productIds.size() == 0) { return null; } if (productIds.size() > 100) { logger.warn("Not recommended to use the readProductsByIds method for long lists of productIds, since " + "Hibernate is required to transform the distinct results. The list of requested" + "product ids was (" + productIds.size() +") in length."); } // Set up the criteria query that specifies we want to return Products CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = builder.createQuery(Product.class); Root<ProductImpl> product = criteria.from(ProductImpl.class); FetchParent fetchParent = product.fetch("defaultSku", JoinType.LEFT); if (!dialectHelper.isOracle() && !dialectHelper.isSqlServer()) { fetchParent.fetch("skuMedia", JoinType.LEFT); } criteria.select(product); // We only want results that match the product IDs criteria.where(product.get("id").as(Long.class).in( sandBoxHelper.mergeCloneIds(ProductImpl.class, productIds.toArray(new Long[productIds.size()])))); if (!dialectHelper.isOracle() && !dialectHelper.isSqlServer()) { criteria.distinct(true); } TypedQuery<Product> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog"); return query.getResultList(); }
public static Iterable<Fetch<?,?>> getAllFetches(FetchParent<?, ?> parent) { return flatMap(new Transformer<Fetch<?,?>,Iterable<Fetch<?,?>>>() { @Override public Iterable<Fetch<?, ?>> transform(Fetch<?, ?> source) { return cons(source, getAllFetches(source)); } }, parent.getFetches()); }
private static void copyFetches(FetchParent<?, ?> from, FetchParent<?, ?> to) { for (Fetch<?, ?> fetch : sort(fetchComparator, from.getFetches())) { Attribute<?, ?> attr = fetch.getAttribute(); @SuppressWarnings({ "rawtypes", "unchecked" }) Fetch<?, ?> f = attr instanceof SingularAttribute ? to.fetch((SingularAttribute) fetch.getAttribute(), fetch.getJoinType()) : attr instanceof CollectionAttribute ? to.fetch((CollectionAttribute) fetch.getAttribute(), fetch.getJoinType()) : attr instanceof SetAttribute ? to.fetch((SetAttribute) fetch.getAttribute(), fetch.getJoinType()) : attr instanceof ListAttribute ? to.fetch((ListAttribute) fetch.getAttribute(), fetch.getJoinType()) : attr instanceof MapAttribute ? to.fetch((MapAttribute) fetch.getAttribute(), fetch.getJoinType()) : to.fetch((CollectionAttribute) fetch.getAttribute(), fetch.getJoinType()); copyFetches(fetch, f); } }