// Add join from the join table to the root element table
DatastoreIdentifier targetTableIdentifier = null;
DatastoreIdentifier rootElementTblId = idFactory.newIdentifier(IdentifierType.TABLE, "ELEMENT");
LogicSetExpression rootElementTblExpr = stmt.newTableExpression(candidateTable, rootElementTblId);
ScalarExpression sourceExpr = sourceMapping.newScalarExpression(stmt, stmt.getMainTableExpression());
ScalarExpression rootElementExpr =
candidateTable.getIDMapping().newScalarExpression(stmt, rootElementTblExpr);
if (allowNull)
{
stmt.leftOuterJoin(sourceExpr, rootElementExpr, rootElementTblExpr, true);
}
else
{
stmt.innerJoin(sourceExpr, rootElementExpr, rootElementTblExpr, true);
}
targetTableIdentifier = rootElementTblId;
if (targetElementTable != candidateTable)
{
// Add join from the root element table to the target element table
DatastoreIdentifier tgtElementTblId = idFactory.newIdentifier(IdentifierType.TABLE, "ELMNTSUB");
LogicSetExpression tgtElementTblExpr = stmt.newTableExpression(targetElementTable, tgtElementTblId);
ScalarExpression targetExpr =
targetElementTable.getIDMapping().newScalarExpression(stmt, tgtElementTblExpr);
if (allowNull)
{
stmt.leftOuterJoin(rootElementExpr, targetExpr, tgtElementTblExpr, true);
}
else
{
stmt.innerJoin(rootElementExpr, targetExpr, tgtElementTblExpr, true);
}
targetTableIdentifier = tgtElementTblId;
}
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();
}
ScalarExpression discrExpr = discriminatorMapping.newScalarExpression(stmt, discriminatorTableExpr);
ScalarExpression discrVal = discriminatorMapping.newLiteral(stmt, discriminatorValue);
stmt.andCondition(discrExpr.eq(discrVal));
}
if (withMetadata == null || withMetadata.booleanValue())
{