if ( path == null )
throw new IllegalStateException( "path = null !" );
// the class for the join is even this class
// or one of the base classes
JDOClassDescriptor sourceClass = _clsDesc;
for ( int i = 1; i < path.size() - 1; i++ ) {
JDOFieldDescriptor fieldDesc = null;
// Find the sourceclass and the fielsddescriptor
// in the class hierachie
Object[] fieldAndClass = getFieldAndClassDesc((String) path.elementAt(i), sourceClass,
_queryExpr, path, i - 1);
if (fieldAndClass == null) {
throw new IllegalStateException( "Field not found:" + path.elementAt(i));
}
fieldDesc = (JDOFieldDescriptor) fieldAndClass[0];
sourceClass = (JDOClassDescriptor) fieldAndClass[1];
JDOClassDescriptor clsDesc = (JDOClassDescriptor) fieldDesc.getClassDescriptor();
if ( clsDesc != null /*&& clsDesc != sourceClass*/ ) {
//we must add this table as a join
if ( fieldDesc.getManyKey() == null ) {
//a many -> one relationship
JDOFieldDescriptor foreignKey = (JDOFieldDescriptor) clsDesc.getIdentity();
String sourceTableAlias = sourceClass.getTableName();
if ( i > 1 )
sourceTableAlias = buildTableAlias( sourceTableAlias, path, i - 1 );
_queryExpr.addInnerJoin( sourceClass.getTableName(),
fieldDesc.getSQLName(),
sourceTableAlias,
clsDesc.getTableName(),
foreignKey.getSQLName(),
buildTableAlias( clsDesc.getTableName(), path, i ) );
} else if ( fieldDesc.getManyTable() == null ) {
//a one -> many relationship
JDOFieldDescriptor identity = (JDOFieldDescriptor) sourceClass.getIdentity();
String sourceTableAlias = sourceClass.getTableName();
if ( i > 1 )
sourceTableAlias = buildTableAlias( sourceTableAlias, path, i - 1 );
_queryExpr.addInnerJoin( sourceClass.getTableName(),
identity.getSQLName(),
sourceTableAlias,
clsDesc.getTableName(),
fieldDesc.getManyKey(),
buildTableAlias( clsDesc.getTableName(), path, i ) );
} else {
//a many -> many relationship
JDOFieldDescriptor identity = (JDOFieldDescriptor) sourceClass.getIdentity();
JDOFieldDescriptor foreignKey = (JDOFieldDescriptor) clsDesc.getIdentity();
String manyTableAlias = fieldDesc.getManyTable();
String sourceTableAlias = sourceClass.getTableName();
if ( i > 1 ) {
manyTableAlias = buildTableAlias( manyTableAlias, path, i - 1 );
sourceTableAlias = buildTableAlias( sourceTableAlias, path, i - 1 );
}
_queryExpr.addInnerJoin( sourceClass.getTableName(),
identity.getSQLName(),
sourceTableAlias,
fieldDesc.getManyTable(),
fieldDesc.getManyKey(),
manyTableAlias);
_queryExpr.addInnerJoin( fieldDesc.getManyTable(),
fieldDesc.getSQLName(),
manyTableAlias,
clsDesc.getTableName(),
foreignKey.getSQLName(),
buildTableAlias( clsDesc.getTableName(), path, i ) );
}
sourceClass = clsDesc;
}
}
}