Source:
public Query build() { Class<?> parentType = relationship.getParentType(); Class<?> idType = dictionary.getIdType(parentType); Object idVal = CoerceUtil.coerce(dictionary.getId(relationship.getParent()), idType); String idField = dictionary.getIdFieldName(parentType); FilterPredicate idExpression = new FilterPredicate( new FilterPredicate.PathElement( parentType, dictionary.getEntityFor(parentType), idType, idField), Operator.IN, Collections.singletonList(idVal)); Collection<FilterPredicate> predicates = new ArrayList<>(); String joinClause = ""; String filterClause = ""; FilterExpression joinedExpression = idExpression; String relationshipName = relationship.getRelationshipName(); String parentAlias = FilterPredicate.getTypeAlias(parentType); String relationshipAlias = parentAlias + UNDERSCORE + relationshipName; if (filterExpression.isPresent()) { ExpressionCloneVisitor cloner = new ExpressionCloneVisitor(); FilterExpression copy = filterExpression.get().accept(cloner); PredicateExtractionVisitor extractor = new PredicateExtractionVisitor(new ArrayList<>()); predicates = copy.accept(extractor); predicates.stream().forEach(predicate -> { predicate.getPath().add(0, new FilterPredicate.PathElement( parentType, dictionary.getEntityFor(parentType), relationship.getChildType(), relationship.getRelationshipName())); }); predicates.add(idExpression); joinedExpression = new AndFilterExpression(copy, idExpression); joinClause = getJoinClauseFromFilters(joinedExpression); filterClause = new HQLFilterOperation().apply(joinedExpression, USE_ALIAS); } else { joinClause = JOIN + parentAlias + PERIOD + relationshipName + SPACE + relationshipAlias + SPACE; filterClause = new HQLFilterOperation().apply(idExpression, USE_ALIAS); predicates.add(idExpression); } Query query = session.createQuery( "SELECT COUNT(DISTINCT " + relationshipAlias + ") " + FROM + parentType.getCanonicalName() + AS + parentAlias + SPACE + joinClause + SPACE + filterClause); supplyFilterQueryParameters(query, predicates); return query; }

Target:
public Query build() { Class<?> parentType = relationship.getParentType(); Class<?> idType = dictionary.getIdType(parentType); Object idVal = CoerceUtil.coerce(dictionary.getId(relationship.getParent()), idType); String idField = dictionary.getIdFieldName(parentType); FilterPredicate idExpression = new FilterPredicate( new FilterPredicate.PathElement( parentType, idType, idField), Operator.IN, Collections.singletonList(idVal)); Collection<FilterPredicate> predicates = new ArrayList<>(); String joinClause = ""; String filterClause = ""; FilterExpression joinedExpression = idExpression; String relationshipName = relationship.getRelationshipName(); String parentAlias = FilterPredicate.getTypeAlias(parentType); String relationshipAlias = parentAlias + UNDERSCORE + relationshipName; if (filterExpression.isPresent()) { ExpressionCloneVisitor cloner = new ExpressionCloneVisitor(); FilterExpression copy = filterExpression.get().accept(cloner); PredicateExtractionVisitor extractor = new PredicateExtractionVisitor(new ArrayList<>()); predicates = copy.accept(extractor); predicates.stream().forEach(predicate -> { predicate.getPath().add(0, new FilterPredicate.PathElement( parentType, relationship.getChildType(), relationship.getRelationshipName())); }); predicates.add(idExpression); joinedExpression = new AndFilterExpression(copy, idExpression); joinClause = getJoinClauseFromFilters(joinedExpression); filterClause = new HQLFilterOperation().apply(joinedExpression, USE_ALIAS); } else { joinClause = JOIN + parentAlias + PERIOD + relationshipName + SPACE + relationshipAlias + SPACE; filterClause = new HQLFilterOperation().apply(idExpression, USE_ALIAS); predicates.add(idExpression); } Query query = session.createQuery( "SELECT COUNT(DISTINCT " + relationshipAlias + ") " + FROM + parentType.getCanonicalName() + AS + parentAlias + SPACE + joinClause + SPACE + filterClause); supplyFilterQueryParameters(query, predicates); return query; }