Getting Total Count Using Criteria Query

  public Page<Role> findByFilter(List<Filter> filters, List<String> permissionCodes, Boolean markForDelete,
      Pageable pageable) {
    CriteriaBuilder builder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Role> query = builder.createQuery(Role.class);
    Root<Role> root = query.from(Role.class);
    Join<Role, Permission> permissionJoin = root.join(Role.PERMISSIONS); 

    Predicate queryPredicate = builder.conjunction();
    queryPredicate = Optional.of(queryPredicate)
        .map(predicate -> this.applySearchFilter(builder, predicate, root, filters))
        .map(predicate -> this.applyInFilter(builder, predicate, permissionJoin.get(Permission.CODE), permissionCodes))
        .map(predicate -> this.applyEqualFilter(builder, predicate, root.get(BaseEntity.MARK_FOR_DELETE), markForDelete))
        .orElse(queryPredicate);
    query.where(queryPredicate);
    
    query.orderBy(Arrays.asList(builder.asc(root.get(BaseEntity.MARK_FOR_DELETE)),
        getSort(builder, root, pageable.getSort().iterator().next())));
    TypedQuery<Role> typedQuery = entityManager.createQuery(query);
    Set<Role> results = getUniqueResults(typedQuery.getResultList());
    long count = entityManager.createQuery(getTotalCount(builder, Role.class, queryPredicate)).getSingleResult();
    return new PageImpl<>(getPageResult(results, pageable), pageable, count);
  }
  protected  CriteriaQuery<Long> getTotalCount (CriteriaBuilder builder, Class className, Predicate queryPredicate) {
    CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
    countQuery.select(builder.count(countQuery.from(className)));
    return countQuery.where(queryPredicate);
  }

getting following error for the above piece of code can some one help

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: ‘generatedAlias1.code’ [select count(generatedAlias0) from com.gdn.athens.core.entity.Role as generatedAlias0 where ( ( 1=1 ) and ( 1=1 ) ) and ( generatedAlias1.code in (:param0) )]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74)
at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:192)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:144)
at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:113)
at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:73)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:155)
at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:604)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:716)
… 69 more

You can’t add a predicate to a query that refers to joins of another unrelated query. You will have to do the joins again in that count query i.e. essentially you will have to rebuild the predicate.