}
if (query.getReferenceClass() == null){
if (this.where != null && ((InternalSelection) this.where).getCurrentNode() != null && ((InternalSelection) this.where).getCurrentNode().getBuilder() != null && ((InternalSelection) this.where).getCurrentNode().getBuilder().getQueryClass() != null) {
query.setReferenceClass(((InternalSelection) this.where).getCurrentNode().getBuilder().getQueryClass());
} else if (roots != null && ! roots.isEmpty()){
Root root = this.getRoots().iterator().next();
query.setReferenceClass(root.getJavaType());
}
}
if (selection == null) {
//the builder in the where clause may not be the correct builder for this query. Search for a root that matches the query type.
if (roots != null && ! roots.isEmpty()){
for (Root root : this.getRoots()){
if (root.getJavaType().equals(this.queryType)){
query.setExpressionBuilder(((RootImpl) root).getCurrentNode().getBuilder());
break;
}
}
}