private <T> Path<T> convertPathForOrder(Path<T> path) {
        PathMetadata metadata = path.getMetadata();
        // at least three levels
        if (metadata.getParent() != null && !metadata.getParent().getMetadata().isRoot()) {
            Set<Expression<?>> exprs = new HashSet<>();
            QueryMetadata md = getMetadata();
            exprs.addAll(md.getGroupBy());
            if (md.getProjection() != null) {
                exprs.add(md.getProjection());
            }
            if (md.getWhere() != null) {
                exprs.add(md.getWhere());
            }
            if (md.getHaving() != null) {
                exprs.add(md.getHaving());
            }
            List<Path<?>> paths = new ArrayList<>();
            // extract paths
            PathsExtractor.DEFAULT.visit(exprs, paths);

            if (!paths.contains(path) && !paths.contains(metadata.getParent())) {
                Path<?> shortened = shorten(metadata.getParent(), paths);
                return ExpressionUtils.path(path.getType(),
                        new PathMetadata(shortened, metadata.getElement(), metadata.getPathType()));
            } else {
                return path;
            }
        } else {
            return path;
        }
    }
{@code JPAQueryMixin} extends {@link QueryMixin} to support JPQL join construction

@author tiwe

@param <T>
querydsl-jpa/src/main/java/com/querydsl/jpa/JPAQueryMixin.java