1 56 package org.objectstyle.cayenne.query; 57 58 import java.util.Collection ; 59 import java.util.Collections ; 60 61 import org.objectstyle.cayenne.CayenneRuntimeException; 62 import org.objectstyle.cayenne.DataObject; 63 import org.objectstyle.cayenne.exp.Expression; 64 import org.objectstyle.cayenne.exp.ExpressionFactory; 65 import org.objectstyle.cayenne.map.Entity; 66 import org.objectstyle.cayenne.map.ObjEntity; 67 import org.objectstyle.cayenne.map.ObjRelationship; 68 69 76 public class RelationshipQuery extends AbstractQuery implements GenericSelectQuery { 77 78 protected DataObject object; 79 80 protected ObjRelationship relationship; 82 83 87 public RelationshipQuery(DataObject object, String relationshipName) { 88 Entity entity = object.getDataContext().getEntityResolver().lookupObjEntity( 89 object); 90 91 ObjRelationship relationship = (ObjRelationship) entity 92 .getRelationship(relationshipName); 93 94 if (relationship == null) { 95 throw new CayenneRuntimeException("No relationship named " 96 + relationshipName 97 + " found in entity " 98 + entity); 99 } 100 101 super.setRoot(relationship.getTargetEntity()); 102 103 this.object = object; 104 this.relationship = relationship; 105 } 106 107 public RelationshipQuery(DataObject object, ObjRelationship relationship) { 108 109 super.setRoot(relationship.getTargetEntity()); 110 111 this.object = object; 112 this.relationship = relationship; 113 } 114 115 public ObjRelationship getRelationship() { 116 return relationship; 117 } 118 119 public DataObject getObject() { 120 return object; 121 } 122 123 public String getCachePolicy() { 124 return GenericSelectQuery.NO_CACHE; 125 } 126 127 public boolean isFetchingDataRows() { 128 return false; 129 } 130 131 public boolean isRefreshingObjects() { 132 return true; 133 } 134 135 public boolean isResolvingInherited() { 136 return true; 137 } 138 139 public int getPageSize() { 140 return 0; 141 } 142 143 public int getFetchLimit() { 144 return 0; 145 } 146 147 public Collection getJointPrefetches() { 148 return Collections.EMPTY_SET; 149 } 150 151 155 public SQLAction createSQLAction(SQLActionVisitor visitor) { 156 157 159 ObjEntity targetEntity = (ObjEntity) relationship.getTargetEntity(); 160 Expression qualifier = ExpressionFactory.matchDbExp(relationship 161 .getReverseDbRelationshipPath(), object); 162 163 SelectQuery select = new SelectQuery(targetEntity, qualifier); 164 165 select.setRefreshingObjects(isRefreshingObjects()); 167 select.setResolvingInherited(isResolvingInherited()); 168 select.setPageSize(getPageSize()); 169 select.setFetchLimit(getFetchLimit()); 170 select.setFetchingDataRows(isFetchingDataRows()); 171 172 return select.createSQLAction(visitor); 174 } 175 } 176 | Popular Tags |