// If the derived expression is a class (non direct collection), then create a new expression
// builder for it, this builder will be compared as equal to the outer reference
if ((mapping != null) && (mapping.getReferenceDescriptor() != null)) {
Expression expression = new ExpressionBuilder(mapping.getReferenceDescriptor().getJavaClass());
queryContext.addUsedIdentificationVariable(rootPath);
queryContext.addQueryExpression(rootPath, expression);
// Check to see if the Expression exists in the parent's context,
// if not, then create/cache a new instance
JPQLQueryContext parentContext = queryContext.getParent();
if (parentContext.getQueryExpressionImp(rootPath) == null) {
parentContext.addQueryExpressionImp(rootPath, queryContext.buildExpression(baseExpression));
}
return expression;
}
else {
// Otherwise it will be derived by duplicating the join to the outer builder inside the
// sub select. The same could be done for direct collection, but difficult to create a
// builder on a table. Retrieve the superquery identification variable from the derived path
int index = rootPath.indexOf('.');
String superqueryVariableName = rootPath.substring(0, index).toUpperCase().intern();
// Create the local ExpressionBuilder for the super identification variable
Expression expression = queryContext.getParent().findQueryExpressionImp(superqueryVariableName);
expression = new ExpressionBuilder(expression.getBuilder().getQueryClass());
queryContext.addUsedIdentificationVariable(superqueryVariableName);
queryContext.addQueryExpression(superqueryVariableName, expression);
// Now create the base Expression for the actual derived path
return queryContext.buildExpression(baseExpression);