In below method, restrictions coming with join and fetch lists. I want to create countQuery where i want to copy the joins and fetch statements, and make join with fetch as false in my count query.
'public Integer getTotalCount(com.sfnt.ems.dao.common.Restrictions<T> restrictions) {
CriteriaBuilder builder = getSession().getCriteriaBuilder();
CriteriaQuery<T> criteriaQuery=restrictions.getCriteriaQuery();
Predicate finalPredicate=criteriaQuery.getRestriction();
Set<Root<?>> roots = criteriaQuery.getRoots();
Root<?> root = roots.iterator().next();
SqmSelectStatement countQuery = (SqmSelectStatement) builder.createQuery(Long.class);
Root<T> rootCountQuery = countQuery.from(getPersistentClass());
countQuery.select(root);
SqmSubQuery sqmSubQuery = (SqmSubQuery<Tuple>) countQuery.subquery(Tuple.class);
SqmSelectStatement sqmOriginalQuery = (SqmSelectStatement) criteriaQuery;
SqmQuerySpec sqmOriginalQuerySpec = sqmOriginalQuery.getQuerySpec();
sqmOriginalQuerySpec.setSortSpecifications(Collections.emptyList());
countQuery.setQueryPart(sqmOriginalQuerySpec.copy(SqmCopyContext.simpleContext()));
countQuery.select(builder.count(builder.literal(1)));
Query query = getSession().createQuery(countQuery);
Long count = (Long) HQLQuery.getUniqueResult(query);
return count.intValue();
}'
Kindly help. Exception facing is SemanticException: query specified join fetching, but the owner of the fetched association was not present in the select list.
As a solution I am trying to iterate the list of joins and set isFetched as false somehow.
'AbstractSqmFrom rootSelection = (AbstractSqmFrom)criteriaQuery.getSelection();
List selectionJoins= rootSelection.getSqmJoins();
for(SqmJoin j:selectionJoins) {
System.out.println(j.getSqmJoinType()));
} ’
List here giving me normal joins and fetch-join both.