String rightTblIdName = (joinExpr.getAlias() != null ? joinExpr.getAlias() : "UNKNOWN_ALIAS");
DatastoreIdentifier rightTblId =
srm.getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE, rightTblIdName);
// Find table expression for left hand side of join
LogicSetExpression leftTableExpr = leftAliasInfo.tableExpression;
if (leftTableExpr == null)
{
if (leftAlias.equalsIgnoreCase(candidateAlias)) // JPQL identifiers are case insensitive
{
// Field of candidate
leftTableExpr = qs.getMainTableExpression();
leftAliasInfo.tableExpression = leftTableExpr; // Set the table expression now we know it
}
else
{
// TODO left side is an alias of something other than the candidate
throw new JPOXUserException("JPOX doesnt yet support joins to non-candidate aliases");
}
}
AbstractMemberMetaData[] rightMmds = leftMmd.getRelatedMemberMetaData(clr);
AbstractMemberMetaData rightMmd = (rightMmds != null && rightMmds.length > 0 ? rightMmds[0] : null);
// TODO Check if right table already exists in "qs" (shouldn't since just starting "qs")
if (leftMapping instanceof PersistenceCapableMapping)
{
// 1-1, N-1 relation field
DatastoreClass rightTable = srm.getDatastoreClass(leftMmd.getTypeName(), clr);
LogicSetExpression rightTblExpr = qs.newTableExpression(rightTable, rightTblId);
if (relationType == Relation.ONE_TO_ONE_UNI ||
(relationType == Relation.ONE_TO_ONE_BI && leftMmd.getMappedBy() == null))
{
// 1-1 FK on this side [join left[FK]->right[ID])
ScalarExpression leftExpr = leftTableExpr.newFieldExpression(leftMmd.getName());
ScalarExpression rightExpr =
rightTable.getIDMapping().newScalarExpression(qs, rightTblExpr);
if (joinExpr.isLeftJoin())
{
qs.leftOuterJoin(leftExpr, rightExpr, rightTblExpr, true, true);
}
else
{
qs.innerJoin(leftExpr, rightExpr, rightTblExpr, true, true);
}
}
else if (relationType == Relation.ONE_TO_ONE_BI && leftMmd.getMappedBy() != null)
{
// 1-1 FK on other side [join left[ID]->right[FK])
ScalarExpression leftExpr =
leftTable.getIDMapping().newScalarExpression(qs, leftTableExpr);
ScalarExpression rightExpr = rightTblExpr.newFieldExpression(rightMmd.getName());
if (joinExpr.isLeftJoin())
{
qs.leftOuterJoin(leftExpr, rightExpr, rightTblExpr, true, true);
}
else
{
qs.innerJoin(leftExpr, rightExpr, rightTblExpr, true, true);
}
}
else if (relationType == Relation.MANY_TO_ONE_BI)
{
if (rightMmd.getJoinMetaData() != null || leftMmd.getJoinMetaData() != null)
{
// Join Table N-1 [join left[ID]->centre(ID_FK), centre(ID_OWN)->right[ID])
ScalarExpression leftExpr =
leftTable.getIDMapping().newScalarExpression(qs, leftTableExpr);
ScalarExpression rightExpr =
rightTable.getIDMapping().newScalarExpression(qs, rightTblExpr);
CollectionTable joinTbl = (CollectionTable)srm.getDatastoreContainerObject(rightMmd);
String joinTblIdName = rightTblIdName + "." + leftAlias;
DatastoreIdentifier joinTblId =
srm.getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE, joinTblIdName);
LogicSetExpression joinTblExpr = qs.newTableExpression(joinTbl, joinTblId);
ScalarExpression joinLeftExpr =
joinTbl.getElementMapping().newScalarExpression(qs, joinTblExpr);
ScalarExpression joinRightExpr =
joinTbl.getOwnerMapping().newScalarExpression(qs, joinTblExpr);
if (joinExpr.isLeftJoin())
{
qs.leftOuterJoin(leftExpr, joinLeftExpr, joinTblExpr, true, true);
qs.innerJoin(joinRightExpr, rightExpr, rightTblExpr, true, true);
}
else
{
qs.innerJoin(leftExpr, joinLeftExpr, joinTblExpr, true, true);
qs.innerJoin(joinRightExpr, rightExpr, rightTblExpr, true, true);
}
}
else
{
// FK N-1 [join left[FK]->right[ID])
ScalarExpression leftExpr = leftTableExpr.newFieldExpression(leftMmd.getName());
ScalarExpression rightExpr = rightTable.getIDMapping().newScalarExpression(qs, rightTblExpr);
if (joinExpr.isLeftJoin())
{
qs.leftOuterJoin(leftExpr, rightExpr, rightTblExpr, true, true);
}
else
{
qs.innerJoin(leftExpr, rightExpr, rightTblExpr, true, true);
}
}
}
if (joinExpr.getAlias() != null)
{
AliasJoinInformation rightAliasInfo = new AliasJoinInformation(joinExpr.getAlias().toUpperCase(),
leftMmd.getType(), rightTblExpr, false);
aliases.put(rightAliasInfo.alias, rightAliasInfo);
}
}
else if (leftMapping instanceof CollectionMapping)
{
// 1-N, M-N collection (element) field
DatastoreClass rightTable =
srm.getDatastoreClass(leftMmd.getCollection().getElementType(), clr);
LogicSetExpression rightTblExpr = qs.newTableExpression(rightTable, rightTblId);
if (relationType == Relation.MANY_TO_MANY_BI || leftMmd.getJoinMetaData() != null)
{
// TODO Cater for 1-N with join specified at other side
// 1-N uni/bi JoinTable relation [join left[ID]->centre(ID_OWN), centre(ID_FK)->right[ID])
ScalarExpression leftExpr =
leftTable.getIDMapping().newScalarExpression(qs, leftTableExpr);
ScalarExpression rightExpr =
rightTable.getIDMapping().newScalarExpression(qs, rightTblExpr);
CollectionTable joinTbl = (CollectionTable)srm.getDatastoreContainerObject(leftMmd);
String joinTblIdName = leftAlias + "." + rightTblIdName;
DatastoreIdentifier joinTblId =
srm.getIdentifierFactory().newIdentifier(IdentifierFactory.TABLE, joinTblIdName);
LogicSetExpression joinTblExpr = qs.newTableExpression(joinTbl, joinTblId);
ScalarExpression joinLeftExpr =
joinTbl.getOwnerMapping().newScalarExpression(qs, joinTblExpr);
ScalarExpression joinRightExpr =
joinTbl.getElementMapping().newScalarExpression(qs, joinTblExpr);
if (joinExpr.isLeftJoin())