// Now the reference class of the query needs to be reversed.
// See the bug description for an explanation.
ExpressionBuilder countBuilder = baseExp.getBuilder();
ExpressionBuilder outerBuilder = new ExpressionBuilder();
ReportQuery subSelect = new ReportQuery(newDescriptor.getJavaClass(), countBuilder);
subSelect.setShouldRetrieveFirstPrimaryKey(true);
// Make sure the outerBuilder does not appear on the left of the
// subselect.
// Putting a builder on the left is desirable to trigger an
// optimization.
subSelect.setSelectionCriteria(baseExp.equal(outerBuilder));
SubSelectExpression sub = new SubSelectExpression(subSelect, ((BaseExpression) baseExp).getBaseExpression());
this.setBaseExpression(outerBuilder);
this.children.setElementAt(outerBuilder, 0);
} else {
// case 4: composite PK, DISTINCT, outer join =>