This was asked numerous times before:
- Hibernate 6.1.6: Using jakarta criteria and count query, getting exception in count query execution if count query contains where predicate - #4 by beikov
- Getting Total Count Using Criteria Query
You have to copy this differently i.e. with the SqmStatement#copy
method. Look at the definition of CriteriaQuery#getRoots()
, it says mutations to the list shall not alter the query.