/** * Creates a ObjectSelect that fetches data for {@link ObjEntity} determined * from provided "entityName", but fetches the result of a provided type. * This factory method is most often used with generic classes that by * themselves are not enough to resolve the entity to fetch. */ public static <T> ObjectSelect<T> query(Class<T> resultType, String entityName) { return new ObjectSelect<T>().entityName(entityName); }
private Map<TargetAttribute, Map<Object, Object>> fetchRelated( ObjectContext context, Map<TargetAttribute, Set<Object>> fkMap) { if (fkMap.isEmpty()) { return Collections.emptyMap(); } // map of *path* to map of *pk* to *object*... Map<TargetAttribute, Map<Object, Object>> related = new HashMap<>(); fkMap.forEach((a, fks) -> { if (!fks.isEmpty()) { TargetAttribute relatedPk = a.getForeignKey().get().getTarget(); String relatedPath = relatedPk.getNormalizedPath().substring(ASTDbPath.DB_PREFIX.length()); String relatedEntityName = relatedPk.getEntity().getName(); Map<Object, Object> relatedFetched = new HashMap<>((int) (fks.size() / 0.75)); ObjectSelect .query(Object.class) .entityName(relatedEntityName) .where(ExpressionFactory.inDbExp(relatedPath, fks)) .select(context) // map by fk value (which is a PK of the matched object of course) .forEach(r -> relatedFetched.put(Cayenne.pkForObject((Persistent) r), r)); related.put(a, relatedFetched); } }); return related; }