code: public static <T> Root<T> findRoot(CriteriaQuery<?> query, Class<T> clazz) {
  
          for (Root<?> r : query.getRoots()) {
              if (clazz.equals(r.getJavaType())) {
                  return (Root<T>) r.as(clazz);
              }
          }
          return null;
      }
docs: Find the Root with type class on CriteriaQuery Root Set
  
  @param <T>   root type
  @param query criteria query
  @param clazz root type
  @return Root<T> of null if none