/** * Return whether the given {@link From} contains a fetch declaration for the attribute with the given name. * * @param from the {@link From} to check for fetches. * @param attribute the attribute name to check. * @return */ private static boolean isAlreadyFetched(From<?, ?> from, String attribute) { for (Fetch<?, ?> fetch : from.getFetches()) { boolean sameName = fetch.getAttribute().getName().equals(attribute); if (sameName && fetch.getJoinType().equals(JoinType.LEFT)) { return true; } } return false; }
@Override public Attribute<? super Z, ?> getAttribute() { return getWrapped().getAttribute(); }
protected void evalFetchJoin(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) { List<String> iPaths = new ArrayList<String>(); List<String> oPaths = new ArrayList<String>(); Set<Root<?>> roots = q.getRoots(); for (Root root : roots) { Set<Fetch> fetches = root.getFetches(); if (fetches == null) continue; for (Fetch<?,?> fetch : fetches) { String fPath = ((Members.Member<?, ?>)fetch.getAttribute()).fmd.getFullName(false); oPaths.add(fPath); if (fetch.getJoinType() == JoinType.INNER) { iPaths.add(fPath); } } } if (!iPaths.isEmpty()) { exps.fetchInnerPaths = iPaths.toArray(new String[iPaths.size()]); } if (!oPaths.isEmpty()) { exps.fetchPaths = oPaths.toArray(new String[oPaths.size()]); } }
protected void evalFetchJoin(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) { List<String> iPaths = new ArrayList<String>(); List<String> oPaths = new ArrayList<String>(); Set<Root<?>> roots = q.getRoots(); for (Root root : roots) { Set<Fetch> fetches = root.getFetches(); if (fetches == null) continue; for (Fetch<?,?> fetch : fetches) { String fPath = ((Members.Member<?, ?>)fetch.getAttribute()).fmd.getFullName(false); oPaths.add(fPath); if (fetch.getJoinType() == JoinType.INNER) { iPaths.add(fPath); } } } if (!iPaths.isEmpty()) { exps.fetchInnerPaths = iPaths.toArray(new String[iPaths.size()]); } if (!oPaths.isEmpty()) { exps.fetchPaths = oPaths.toArray(new String[oPaths.size()]); } }
protected void evalFetchJoin(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) { List<String> iPaths = new ArrayList<String>(); List<String> oPaths = new ArrayList<String>(); Set<Root<?>> roots = q.getRoots(); for (Root root : roots) { Set<Fetch> fetches = root.getFetches(); if (fetches == null) continue; for (Fetch<?,?> fetch : fetches) { String fPath = ((Members.Member<?, ?>)fetch.getAttribute()).fmd.getFullName(false); oPaths.add(fPath); if (fetch.getJoinType() == JoinType.INNER) { iPaths.add(fPath); } } } if (!iPaths.isEmpty()) { exps.fetchInnerPaths = iPaths.toArray(new String[iPaths.size()]); } if (!oPaths.isEmpty()) { exps.fetchPaths = oPaths.toArray(new String[oPaths.size()]); } }
protected void evalFetchJoin(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) { List<String> iPaths = new ArrayList<String>(); List<String> oPaths = new ArrayList<String>(); Set<Root<?>> roots = q.getRoots(); for (Root root : roots) { Set<Fetch> fetches = root.getFetches(); if (fetches == null) continue; for (Fetch<?,?> fetch : fetches) { String fPath = ((Members.Member<?, ?>)fetch.getAttribute()).fmd.getFullName(false); oPaths.add(fPath); if (fetch.getJoinType() == JoinType.INNER) { iPaths.add(fPath); } } } if (!iPaths.isEmpty()) { exps.fetchInnerPaths = iPaths.toArray(new String[iPaths.size()]); } if (!oPaths.isEmpty()) { exps.fetchPaths = oPaths.toArray(new String[oPaths.size()]); } }
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); }
private static void collectJoins(Path<?> path, Map<String, Path<?>> joins) { if (path instanceof From) { ((From<?, ?>) path).getJoins().forEach(join -> collectJoins(join, joins)); } if (path instanceof FetchParent) { try { ((FetchParent<?, ?>) path).getFetches().stream().filter(fetch -> fetch instanceof Path).forEach(fetch -> collectJoins((Path<?>) fetch, joins)); } catch (NullPointerException openJPAWillThrowThisOnEmptyFetches) { // Ignore and continue. } } if (path instanceof Join) { joins.put(((Join<?, ?>) path).getAttribute().getName(), path); } else if (path instanceof Fetch) { joins.put(((Fetch<?, ?>) path).getAttribute().getName(), path); } }
/** * Return whether the given {@link From} contains a fetch declaration for the attribute with the given name. * * @param from the {@link From} to check for fetches. * @param attribute the attribute name to check. * @return */ private static boolean isAlreadyFetched(From<?, ?> from, String attribute) { for (Fetch<?, ?> fetch : from.getFetches()) { boolean sameName = fetch.getAttribute().getName().equals(attribute); if (sameName && fetch.getJoinType().equals(JoinType.LEFT)) { return true; } } return false; }
private static boolean containsMultiRelationFetch(Set<?> fetches) { for (Object fetchObj : fetches) { Fetch<?, ?> fetch = (Fetch<?, ?>) fetchObj; Attribute<?, ?> attr = fetch.getAttribute(); if (attr.isAssociation() && attr.isCollection()) return true; if (containsMultiRelationFetch(fetch.getFetches())) return true; } return false; }
private static boolean containsMultiRelationJoin(Set<?> fetches) { for (Object fetchObj : fetches) { Fetch<?, ?> fetch = (Fetch<?, ?>) fetchObj; Attribute<?, ?> attr = fetch.getAttribute(); if (attr.isAssociation() && attr.isCollection()) return true; if (containsMultiRelationFetch(fetch.getFetches())) return true; } return false; }
@SuppressWarnings({"unchecked", "rawtypes"}) protected void fetches(SearchParameters sp, Root<E> root) { for (List<Attribute<?, ?>> args : sp.getFetches()) { FetchParent<?, ?> from = root; for (Attribute<?, ?> arg : args) { 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); } } } } } }
/** * Copy Fetches * * @param from source Fetch * @param to dest Fetch */ public static void copyFetches(Fetch<?, ?> from, Fetch<?, ?> to) { for (Fetch<?, ?> f : from.getFetches()) { Fetch<?, ?> toFetch = to.fetch(f.getAttribute().getName()); // recursively copy fetches copyFetches(f, toFetch); } } }
protected void evalAccessPaths(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) { Set<ClassMetaData> metas = new HashSet<ClassMetaData>(); MetamodelImpl metamodel = q.getMetamodel(); for (Root<?> root : q.getRoots()) { metas.add(((AbstractManagedType<?>)root.getModel()).meta); for (Join<?,?> join : root.getJoins()) { Class<?> cls = join.getAttribute().getJavaType(); if (join.getAttribute().isAssociation()) { ClassMetaData meta = metamodel.getRepository().getMetaData(cls, null, true); PersistenceType type = MetamodelImpl.getPersistenceType(meta); if (type == PersistenceType.ENTITY || type == PersistenceType.EMBEDDABLE) metas.add(meta); } } for (Fetch<?,?> fetch : root.getFetches()) { metas.add(metamodel.getRepository().getCachedMetaData(fetch.getAttribute().getJavaType())); } } exps.accessPath = metas.toArray(new ClassMetaData[metas.size()]); }
protected void evalAccessPaths(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) { Set<ClassMetaData> metas = new HashSet<ClassMetaData>(); MetamodelImpl metamodel = q.getMetamodel(); for (Root<?> root : q.getRoots()) { metas.add(((AbstractManagedType<?>)root.getModel()).meta); for (Join<?,?> join : root.getJoins()) { Class<?> cls = join.getAttribute().getJavaType(); if (join.getAttribute().isAssociation()) { ClassMetaData meta = metamodel.getRepository().getMetaData(cls, null, true); PersistenceType type = MetamodelImpl.getPersistenceType(meta); if (type == PersistenceType.ENTITY || type == PersistenceType.EMBEDDABLE) metas.add(meta); } } for (Fetch<?,?> fetch : root.getFetches()) { metas.add(metamodel.getRepository().getCachedMetaData(fetch.getAttribute().getJavaType())); } } exps.accessPath = metas.toArray(new ClassMetaData[metas.size()]); }
Attribute attr = join.getAttribute(); tuples.add(attr.getName()); PrimaryExpression primExpr = new PrimaryExpression(new ArrayList(tuples));
@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 (attribute instanceof PluralAttribute) { path = ((From<?, ?>) path).join(attribute.getName(), JoinType.LEFT); } else { path = path.get(attribute.getName()); } } } return (Path<F>) path; }
/** * Convert the passed propertyPath into a JPA path. * <p> * Note: JPA will do joins if the property is in an associated entity. */ @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 (attribute instanceof PluralAttribute) { path = ((From<?, ?>) path).join(attribute.getName(), JoinType.LEFT); } else { path = path.get(attribute.getName()); } } } return (Path<F>) path; }
/** * Copy Joins * * @param from source Join * @param to destination Join */ public static void copyJoins(From<?, ?> from, From<?, ?> to) { for (Join<?, ?> j : from.getJoins()) { Join<?, ?> toJoin = to.join(j.getAttribute().getName(), j.getJoinType()); toJoin.alias(getOrCreateAlias(j)); copyJoins(j, toJoin); } for (Fetch<?, ?> f : from.getFetches()) { Fetch<?, ?> toFetch = to.fetch(f.getAttribute().getName()); copyFetches(f, toFetch); } }