1 56 package org.objectstyle.cayenne.query; 57 58 import java.util.Iterator ; 59 60 import org.objectstyle.cayenne.CayenneRuntimeException; 61 import org.objectstyle.cayenne.exp.Expression; 62 import org.objectstyle.cayenne.map.EntityInheritanceTree; 63 import org.objectstyle.cayenne.map.EntityResolver; 64 import org.objectstyle.cayenne.map.ObjEntity; 65 import org.objectstyle.cayenne.map.ObjRelationship; 66 67 73 public class PrefetchSelectQuery extends SelectQuery { 74 protected SelectQuery parentQuery; 75 76 79 protected String prefetchPath; 80 81 84 protected ObjRelationship lastPrefetchHint; 85 86 91 public PrefetchSelectQuery( 92 EntityResolver resolver, 93 SelectQuery parentQuery, 94 String prefetch) { 95 96 setParentQuery(parentQuery); 97 setPrefetchPath(prefetch); 98 99 ObjEntity entity = resolver.lookupObjEntity(parentQuery); 100 EntityInheritanceTree inheritanceTree = resolver.lookupInheritanceTree(entity); 101 102 Iterator it = entity.resolvePathComponents(prefetch); 103 104 106 ObjRelationship r = null; 107 while (it.hasNext()) { 108 r = (ObjRelationship) it.next(); 109 } 110 111 if (r == null) { 112 throw new CayenneRuntimeException( 113 "Invalid prefetch '" + prefetch + "' for entity: " + entity.getName()); 114 } 115 116 setRoot(r.getTargetEntity()); 117 118 Expression queryQualifier = parentQuery.getQualifier(); 120 121 Expression entityQualifier = 122 (inheritanceTree != null) 123 ? inheritanceTree.qualifierForEntityAndSubclasses() 124 : entity.getDeclaredQualifier(); 125 126 if (entityQualifier != null) { 127 queryQualifier = 128 (queryQualifier != null) 129 ? queryQualifier.andExp(entityQualifier) 130 : entityQualifier; 131 } 132 133 setQualifier(entity.translateToRelatedEntity(queryQualifier, prefetchPath)); 134 135 if (r.isToMany() && !r.isFlattened()) { 136 setLastPrefetchHint(r); 137 } 138 } 139 140 141 145 public String getPrefetchPath() { 146 return prefetchPath; 147 } 148 149 153 public void setPrefetchPath(String prefetchPath) { 154 this.prefetchPath = prefetchPath; 155 } 156 157 160 public SelectQuery getParentQuery() { 161 return parentQuery; 162 } 163 164 167 public void setParentQuery(SelectQuery parentQuery) { 168 this.parentQuery = parentQuery; 169 } 170 171 174 public ObjRelationship getLastPrefetchHint() { 175 return lastPrefetchHint; 176 } 177 178 181 public void setLastPrefetchHint(ObjRelationship relationship) { 182 lastPrefetchHint = relationship; 183 } 184 } 185 | Popular Tags |