List of usage examples for javax.persistence.criteria FetchParent fetch
@SuppressWarnings("hiding")
<X, Y> Fetch<X, Y> fetch(String attributeName, JoinType jt);
From source file:com.ocs.dynamo.dao.query.JpaQueryBuilder.java
/** * Adds fetch join information to a query root * //from w w w . j ava2 s . c om * @param root * the query root * @param fetchJoins * the fetch joins * @return <code>true</code> if the fetches include a collection, <code>false</code> otherwise */ private static <T> boolean addFetchJoinInformation(FetchParent<T, ?> root, FetchJoinInformation... fetchJoins) { boolean collection = false; if (root != null && fetchJoins != null) { for (FetchJoinInformation s : fetchJoins) { // Support nested properties FetchParent<T, ?> fetch = root; String[] ppath = s.getProperty().split("\\."); for (String prop : ppath) { fetch = fetch.fetch(prop, s.getJoinType()); } } // check if any collection is fetched. If so then the results need // to be cleaned up using "distinct" collection = isCollectionFetch(root); } return collection; }
From source file:org.batoo.jpa.core.impl.criteria.jpql.JpqlQuery.java
/** * Creates the from fragment of the query. * //from w ww.j a v a 2 s .co m * @param cb * the criteria builder * @param q * the query join * @param r * the root * @param joins * the joins metadata * * @since 2.0.0 */ private void constructJoins(CriteriaBuilderImpl cb, AbstractQuery<?> q, RootImpl<Object> r, Tree joins) { for (int i = 0; i < joins.getChildCount(); i++) { final Tree join = joins.getChild(i); JoinType joinType = JoinType.INNER; final int joinSpecification = join.getChild(0).getType(); int offset = 0; if (joinSpecification == JpqlParser.INNER) { offset = 1; joinType = JoinType.INNER; } else if (joinSpecification == JpqlParser.LEFT) { offset = 1; joinType = JoinType.LEFT; } if (join.getChildCount() == (offset + 3)) { FetchParent<?, ?> parent = this.getAliased(q, join.getChild(offset).getText()); final Qualified qualified = new Qualified(join.getChild(offset + 1).getChild(0)); for (final String segment : qualified.getSegments()) { parent = parent.fetch(segment, joinType); } } else { AbstractFrom<?, ?> parent = this.getAliased(q, join.getChild(offset).getText()); final Aliased aliased = new Aliased(join.getChild(offset + 1)); int depth = 0; for (final String segment : aliased.getQualified().getSegments()) { if ((depth > 0) && (parent instanceof PluralJoin)) { throw new PersistenceException( "Cannot qualify, only embeddable joins within the path allowed, " + "line " + join.getLine() + ":" + join.getCharPositionInLine()); } parent = parent.join(segment, joinType); depth++; } parent.alias(aliased.getAlias()); this.putAlias((BaseQuery<?>) q, join.getChild(1), aliased, parent); } } }
From source file:org.batoo.jpa.core.impl.model.EntityTypeImpl.java
private void prepareEagerJoins(FetchParent<?, ?> r, int depth, AssociationMappingImpl<?, ?, ?> parent, JoinedMapping<?, ?, ?>[] mappings) { for (final JoinedMapping<?, ?, ?> mapping : mappings) { // Element collection if (mapping.getMappingType() == MappingType.ELEMENT_COLLECTION) { r.fetch(mapping.getAttribute().getName(), JoinType.LEFT); continue; }/* w w w .jav a 2s. co m*/ // embeddable else if (mapping.getMappingType() == MappingType.EMBEDDABLE) { final Fetch<?, Object> r2 = r.fetch(mapping.getAttribute().getName(), JoinType.LEFT); this.prepareEagerJoins(r2, depth, parent, ((EmbeddedMappingImpl<?, ?>) mapping).getEagerMappings()); continue; } // association else { final AssociationMappingImpl<?, ?, ?> association = (AssociationMappingImpl<?, ?, ?>) mapping; // if we are coming from the inverse side and inverse side is not many-to-one then skip if ((parent != null) && // (association.getInverse() == parent) && // (parent.getAttribute() .getPersistentAttributeType() != PersistentAttributeType.MANY_TO_ONE)) { continue; } // check association's fetch strategy and max depth if ((association.getMaxFetchJoinDepth() < depth) || (association.getFetchStrategy() == FetchStrategyType.SELECT)) { continue; } final Fetch<?, Object> r2 = r.fetch(((AbstractMapping<?, ?, ?>) mapping).getAttribute().getName(), JoinType.LEFT); final EntityTypeImpl<?> type = association.getType(); type.prepareEagerJoins(r2, depth + 1, association); } } }
From source file:org.batoo.jpa.core.impl.model.type.EntityTypeImpl.java
private void prepareEagerJoins(FetchParent<?, ?> r, int depth, AssociationMapping<?, ?, ?> parent, JoinedMapping<?, ?, ?>[] mappings) { for (final JoinedMapping<?, ?, ?> mapping : mappings) { // Element collection if (mapping.getMappingType() == MappingType.ELEMENT_COLLECTION) { r.fetch(mapping.getAttribute().getName(), JoinType.LEFT); continue; }/*from ww w . j a v a2 s .co m*/ // embeddable else if (mapping.getMappingType() == MappingType.EMBEDDABLE) { final Fetch<?, Object> r2 = r.fetch(mapping.getAttribute().getName(), JoinType.LEFT); this.prepareEagerJoins(r2, depth, parent, ((EmbeddedMapping<?, ?>) mapping).getEagerMappings()); continue; } // association else { final AssociationMapping<?, ?, ?> association = (AssociationMapping<?, ?, ?>) mapping; // if we are coming from the inverse side and inverse side is not many-to-one then skip if ((parent != null) && // (association.getInverse() == parent) && // (parent.getAttribute() .getPersistentAttributeType() != PersistentAttributeType.MANY_TO_ONE)) { continue; } final Fetch<?, Object> r2 = r.fetch(mapping.getAttribute().getName(), JoinType.LEFT); final EntityTypeImpl<?> type = association.getType(); type.prepareEagerJoins(r2, depth + 1, association); } } }
From source file:org.broadleafcommerce.core.catalog.dao.ProductDaoImpl.java
@Override public List<Product> readProductsByIds(List<Long> productIds) { if (productIds == null || productIds.size() == 0) { return null; }// ww w . ja va 2 s . c o m 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(); }
From source file:org.sparkcommerce.core.catalog.dao.ProductDaoImpl.java
@Override public List<Product> readProductsByIds(List<Long> productIds) { if (productIds == null || productIds.size() == 0) { return null; }/*from w ww .j a v a2 s.c o m*/ 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(); }