boolean allowNull, DatastoreIdentifier candidateId)
{
QueryExpression stmt;
JavaTypeMapping discriminatorMapping = null;
DiscriminatorMetaData discriminatorMetaData = null;
LogicSetExpression discriminatorTableExpr = null;
if (sourceTable instanceof SCOTable)
{
// * Selecting the join table of a JoinTable (normal) relationship, and joining to the element table
stmt = dba.newQueryStatement(sourceTable, candidateId, clr);
// Add inner joins to all classes above up to elementType and across to the join table
DatastoreIdentifier targetTableIdentifier = joinSourceToTargetElement(stmt, targetElementTable, allowNull);
discriminatorMapping = targetElementTable.getDiscriminatorMapping(false);
discriminatorMetaData = targetElementTable.getDiscriminatorMetaData();
discriminatorTableExpr = stmt.getTableExpression(targetTableIdentifier);
// Add left outer joins to exclude any target element subclasses
if (joinToExcludeTargetSubclasses)
{
joinToExcludeTargetWhenSubElementsExists(stmt, sourceMapping, targetElementType);
}
}
else
{
// * Selecting FCO objects directly (Extents etc)
// * Selecting the element table of a ForeignKey (inverse) relationship
stmt = dba.newQueryStatement(candidateTable, candidateId, clr);
discriminatorMapping = sourceTable.getDiscriminatorMapping(false);
discriminatorMetaData = sourceTable.getDiscriminatorMetaData();
discriminatorTableExpr = stmt.getMainTableExpression();
// in case of the elementType is a subClass of the element for the candidateTable
// if the root (candidate) element type is not the same as the current target element type
// joins the root to the current element
if ((!targetElementTable.toString().equals(sourceTable.toString()) &&
!candidateTable.getType().equals(targetElementType)) ||
sourceJoin)
{
// Add inner joins to all classes above up to elementType and across to the join table
if (sourceJoin)
{
joinTargetToSourceElement(stmt, targetElementTable, false);
}
else
{
joinSourceToTargetElement(stmt, targetElementTable, false);
}
}
if (joinToExcludeTargetSubclasses)
{
joinToExcludeTargetWhenSubElementsExists(stmt, candidateTable.getIDMapping(), targetElementType);
}
}
if (discriminatorMapping != null && discriminatorMetaData.getStrategy() != DiscriminatorStrategy.NONE)
{
// Restrict to valid discriminator values where we have a discriminator specified on this table
String discriminatorValue = targetElementType;
if (discriminatorMetaData.getStrategy() == DiscriminatorStrategy.VALUE_MAP)
{
// Get the MetaData for the target class since that holds the "value"
AbstractClassMetaData targetCmd = storeMgr.getOMFContext().getMetaDataManager().getMetaDataForClass(targetElementType, clr);
discriminatorValue = targetCmd.getInheritanceMetaData().getDiscriminatorMetaData().getValue();
}