String subqueryRootAlias,
ObjRelationship relationship) {
List<DbRelationship> dbRelationships = relationship.getDbRelationships();
// reverse order to get the nearest to the correlated of the direct relation
for (int i = dbRelationships.size() - 1; i > 0; i--) {
DbRelationship dbRelationship = dbRelationships.get(i);
String subqueryTargetTableName = ((DbEntity) dbRelationship.getTargetEntity())
.getFullyQualifiedName();
String subqueryTargetAlias;
if (i == dbRelationships.size() - 1) {
subqueryTargetAlias = subqueryRootAlias;
context.append(subqueryTargetTableName).append(' ').append(
subqueryTargetAlias);
}
else {
subqueryTargetAlias = context.getTableAlias(
subqueryTargetTableName,
subqueryTargetTableName);
}
context.append(" JOIN ");
String subquerySourceTableName = ((DbEntity) dbRelationship.getSourceEntity())
.getFullyQualifiedName();
String subquerySourceAlias = context.getTableAlias(
subquerySourceTableName,
subquerySourceTableName);
context.append(subquerySourceTableName).append(' ').append(
subquerySourceAlias);
context.append(" ON (");
List<DbJoin> joins = dbRelationship.getJoins();
Iterator<DbJoin> it = joins.iterator();
while (it.hasNext()) {
DbJoin join = it.next();
context.append(' ').append(subqueryTargetAlias).append('.').append(
join.getTargetName()).append(" = ");