}
RDBMSStoreManager storeMgr = stmt.getRDBMSManager();
MetaDataManager mmgr = storeMgr.getMetaDataManager();
AbstractMemberMetaData mmd = mapExpr.getJavaTypeMapping().getMemberMetaData();
AbstractClassMetaData keyCmd = mmd.getMap().getKeyClassMetaData(clr, mmgr);
AbstractClassMetaData valCmd = mmd.getMap().getValueClassMetaData(clr, mmgr);
if (mmd.getMap().getMapType() == MapType.MAP_TYPE_JOIN)
{
// Map formed in join table - add join to join table, then to key/value tables (if present)
MapTable mapTbl = (MapTable)storeMgr.getDatastoreContainerObject(mmd);
SQLTable joinSqlTbl = stmt.innerJoin(mapExpr.getSQLTable(), mapExpr.getSQLTable().getTable().getIdMapping(),
mapTbl, null, mapTbl.getOwnerMapping(), null, null);
if (valCmd != null)
{
DatastoreClass valTbl = storeMgr.getDatastoreClass(valCmd.getFullClassName(), clr);
SQLTable valSqlTbl = stmt.innerJoin(joinSqlTbl, mapTbl.getValueMapping(),
valTbl, null, valTbl.getIdMapping(), null, null);
SQLExpression valIdExpr = exprFactory.newExpression(stmt, valSqlTbl, valTbl.getIdMapping());
if (valIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(valVarName, valCmd, valIdExpr.getSQLTable(),
valIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to value
stmt.whereAnd(valIdExpr.eq(valExpr), true);
}
}
else
{
SQLExpression valIdExpr = exprFactory.newExpression(stmt, joinSqlTbl, mapTbl.getValueMapping());
if (valIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(valVarName, valCmd, valIdExpr.getSQLTable(),
valIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to value
stmt.whereAnd(valIdExpr.eq(valExpr), true);
}
}
if (keyCmd != null)
{
DatastoreClass keyTbl = storeMgr.getDatastoreClass(keyCmd.getFullClassName(), clr);
SQLTable keySqlTbl = stmt.innerJoin(joinSqlTbl, mapTbl.getKeyMapping(),
keyTbl, null, keyTbl.getIdMapping(), null, null);
SQLExpression keyIdExpr = exprFactory.newExpression(stmt, keySqlTbl, keyTbl.getIdMapping());
if (keyIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(keyVarName, keyCmd, keyIdExpr.getSQLTable(),
keyIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to key
stmt.whereAnd(keyIdExpr.eq(keyExpr), true);
}
}
else
{
SQLExpression keyIdExpr = exprFactory.newExpression(stmt, joinSqlTbl, mapTbl.getKeyMapping());
if (keyIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(keyVarName, keyCmd, keyIdExpr.getSQLTable(),
keyIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to key
stmt.whereAnd(keyIdExpr.eq(keyExpr), true);
}
}
}
else if (mmd.getMap().getMapType() == MapType.MAP_TYPE_KEY_IN_VALUE)
{
// Map formed in value table - add join to value table, then to key table (if present)
DatastoreClass valTbl = storeMgr.getDatastoreClass(valCmd.getFullClassName(), clr);
JavaTypeMapping ownerMapping = null;
if (mmd.getMappedBy() != null)
{
ownerMapping = valTbl.getMemberMapping(valCmd.getMetaDataForMember(mmd.getMappedBy()));
}
else
{
ownerMapping = valTbl.getExternalMapping(mmd, MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
}
SQLTable valSqlTbl = stmt.innerJoin(mapExpr.getSQLTable(), mapExpr.getSQLTable().getTable().getIdMapping(),
valTbl, null, ownerMapping, null, null);
SQLExpression valIdExpr = exprFactory.newExpression(stmt, valSqlTbl, valTbl.getIdMapping());
if (valIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(valVarName, valCmd, valIdExpr.getSQLTable(),
valIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to value
stmt.whereAnd(valIdExpr.eq(valExpr), true);
}
if (keyCmd != null)
{
// Add inner join to key table
AbstractMemberMetaData valKeyMmd =
valCmd.getMetaDataForMember(mmd.getKeyMetaData().getMappedBy());
DatastoreClass keyTbl = storeMgr.getDatastoreClass(keyCmd.getFullClassName(), clr);
SQLTable keySqlTbl = stmt.innerJoin(valSqlTbl, valTbl.getMemberMapping(valKeyMmd),
keyTbl, null, keyTbl.getIdMapping(), null, null);
SQLExpression keyIdExpr = exprFactory.newExpression(stmt, keySqlTbl, keyTbl.getIdMapping());
if (keyIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(keyVarName, keyCmd, keyIdExpr.getSQLTable(),
keyIdExpr.getJavaTypeMapping());
}
else
{
// Add restriction to key
stmt.whereAnd(keyIdExpr.eq(keyExpr), true);
}
}
else
{
AbstractMemberMetaData valKeyMmd =
valCmd.getMetaDataForMember(mmd.getKeyMetaData().getMappedBy());
SQLExpression keyIdExpr = exprFactory.newExpression(stmt, valSqlTbl,
valTbl.getMemberMapping(valKeyMmd));
if (keyIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(keyVarName, keyCmd, keyIdExpr.getSQLTable(),
keyIdExpr.getJavaTypeMapping());
}
else
{
// Add restriction to key
stmt.whereAnd(keyIdExpr.eq(keyExpr), true);
}
}
}
else if (mmd.getMap().getMapType() == MapType.MAP_TYPE_VALUE_IN_KEY)
{
// Map formed in key table - add join to key table then to value table (if present)
DatastoreClass keyTbl = storeMgr.getDatastoreClass(keyCmd.getFullClassName(), clr);
AbstractMemberMetaData keyValMmd =
keyCmd.getMetaDataForMember(mmd.getValueMetaData().getMappedBy());
JavaTypeMapping ownerMapping = null;
if (mmd.getMappedBy() != null)
{
ownerMapping = keyTbl.getMemberMapping(keyCmd.getMetaDataForMember(mmd.getMappedBy()));
}
else
{
ownerMapping = keyTbl.getExternalMapping(mmd, MappingConsumer.MAPPING_TYPE_EXTERNAL_FK);
}
SQLTable keySqlTbl = stmt.innerJoin(mapExpr.getSQLTable(), mapExpr.getSQLTable().getTable().getIdMapping(),
keyTbl, null, ownerMapping, null, null);
SQLExpression keyIdExpr = exprFactory.newExpression(stmt, keySqlTbl, keyTbl.getIdMapping());
if (keyIsUnbound)
{
// Bind the variable in the QueryGenerator
stmt.getQueryGenerator().bindVariable(keyVarName, keyCmd, keyIdExpr.getSQLTable(),
keyIdExpr.getJavaTypeMapping());
}
else
{
// Add restrict to key
stmt.whereAnd(keyIdExpr.eq(keyExpr), true);
}
if (valCmd != null)
{
// Add inner join to value table
DatastoreClass valTbl = storeMgr.getDatastoreClass(valCmd.getFullClassName(), clr);
SQLTable valSqlTbl = stmt.innerJoin(keySqlTbl, keyTbl.getMemberMapping(keyValMmd),
valTbl, null, valTbl.getIdMapping(), null, null);
SQLExpression valIdExpr = exprFactory.newExpression(stmt, valSqlTbl, valTbl.getIdMapping());
if (valIsUnbound)