RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
AbstractMemberMetaData mmd = collExpr.getJavaTypeMapping().getMemberMetaData();
AbstractClassMetaData elemCmd =
mmd.getCollection().getElementClassMetaData(clr, storeMgr.getMetaDataManager());
CollectionTable joinTbl = (CollectionTable)storeMgr.getDatastoreContainerObject(mmd);
if (elemIsUnbound)
{
Class varType = stmt.getQueryGenerator().getTypeOfVariable(varName);
if (varType != null)
{
elemType = varType.getName();
elemCmd = storeMgr.getMetaDataManager().getMetaDataForClass(elemType, clr);
}
}
if (elemType == null)
{
elemType = mmd.getCollection().getElementType();
}
if (joinTbl != null)
{
// JoinTable Collection - join from owner to join, then from join to element
if (elemCmd == null)
{
// Collection<Non-PC>
SQLTable joinSqlTbl = stmt.innerJoin(collExpr.getSQLTable(), collExpr.getSQLTable().getTable().getIdMapping(),
joinTbl, elemAlias, joinTbl.getOwnerMapping(), null, null);
SQLExpression elemIdExpr = exprFactory.newExpression(stmt, joinSqlTbl, joinTbl.getElementMapping());
if (elemIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(varName, null, elemIdExpr.getSQLTable(),
elemIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to element
stmt.whereAnd(elemIdExpr.eq(elemExpr), true);
}
}
else
{
// Collection<PC>
SQLTable joinSqlTbl = stmt.innerJoin(collExpr.getSQLTable(), collExpr.getSQLTable().getTable().getIdMapping(),
joinTbl, null, joinTbl.getOwnerMapping(), null, null);
if (!mmd.getCollection().isEmbeddedElement())
{
DatastoreClass elemTbl = storeMgr.getDatastoreClass(elemType, clr);
SQLTable elemSqlTbl = null;
if (joinTbl.getElementMapping() instanceof ReferenceMapping &&
((ReferenceMapping)joinTbl.getElementMapping()).getMappingStrategy() == ReferenceMapping.PER_IMPLEMENTATION_MAPPING)
{
JavaTypeMapping elemMapping = null;
JavaTypeMapping[] elemImplMappings = ((ReferenceMapping)joinTbl.getElementMapping()).getJavaTypeMapping();
for (int i=0;i<elemImplMappings.length;i++)
{
if (elemImplMappings[i].getType().equals(elemCmd.getFullClassName()))
{
elemMapping = elemImplMappings[i];
break;
}
}
elemSqlTbl = stmt.innerJoin(joinSqlTbl, elemMapping, joinTbl.getElementMapping(),
elemTbl, elemAlias, elemTbl.getIdMapping(), null, null, null);
}
else
{
elemSqlTbl = stmt.innerJoin(joinSqlTbl, joinTbl.getElementMapping(),
elemTbl, elemAlias, elemTbl.getIdMapping(), null, null);
}
SQLExpression elemIdExpr = exprFactory.newExpression(stmt, elemSqlTbl, elemTbl.getIdMapping());
if (elemIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(varName, elemCmd, elemIdExpr.getSQLTable(),
elemIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to element
stmt.whereAnd(elemIdExpr.eq(elemExpr), true);
}
}
else
{
SQLExpression elemIdExpr = exprFactory.newExpression(stmt, joinSqlTbl, joinTbl.getElementMapping());
if (elemIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(varName, elemCmd, elemIdExpr.getSQLTable(),
elemIdExpr.getJavaTypeMapping());