// find last relationship
Iterator<CayenneMapEntry> it = classDescriptor.getEntity().resolvePathComponents(
prefetchPath);
ObjRelationship relationship = null;
while (it.hasNext()) {
relationship = (ObjRelationship) it.next();
}
if (relationship == null) {
throw new CayenneRuntimeException(
"Invalid prefetch '%s' for entity '%s'",
prefetchPath,
classDescriptor.getEntity().getName());
}
// chain query and entity qualifiers
Expression queryQualifier = query.getQualifier();
Expression entityQualifier = classDescriptor
.getEntityInheritanceTree()
.qualifierForEntityAndSubclasses();
if (entityQualifier != null) {
queryQualifier = (queryQualifier != null) ? queryQualifier
.andExp(entityQualifier) : entityQualifier;
}
// create and configure PrefetchSelectQuery
PrefetchSelectQuery prefetchQuery = new PrefetchSelectQuery(
query,
prefetchPath,
relationship);
prefetchQuery.setQualifier(classDescriptor.getEntity().translateToRelatedEntity(
queryQualifier,
prefetchPath));
if (relationship.isSourceIndependentFromTargetChange()) {
// setup extra result columns to be able to relate result rows to the parent
// result objects.
prefetchQuery.addResultPath("db:"
+ relationship.getReverseDbRelationshipPath());
}
// pass prefetch subtree to enable joint prefetches...
prefetchQuery.setPrefetchTree(node);