Here below is a subquery to retrieve a list of ids:
protected Long getIdList(
@NonNull final CriteriaBuilder rootBuilder,
@NonNull final CriteriaQuery<?> rootQuery,
@NonNull final Pageable pageable) {
final HibernateCriteriaBuilder listBuilder = (HibernateCriteriaBuilder) rootBuilder;
final JpaCriteriaQuery<Long> listQuery = listBuilder.createQuery(Long.class);
final SqmSubQuery<Tuple> subQuery = (SqmSubQuery<Tuple>) listQuery.subquery(Tuple.class);
final SqmQuerySpec<Tuple> rootQuerySpec = ((SqmSelectStatement) rootQuery).getQuerySpec();
final SqmQuerySpec<Tuple> subQuerySpec = rootQuerySpec.copy(SqmCopyContext.simpleContext());
subQuery.setQueryPart(subQuerySpec);
final Root<?> subRoot = subQuery.getRoots().iterator().next();
subQuery.multiselect(subRoot.get("id").alias("id"));
// listQuery.multiselect(listBuilder.count(listBuilder.literal(1))).from(subQuery);
// instead of counting the ids, I want to get them...
listQuery.multiselect(subRoot.get("id")).from(subQuery);
return entityManager
.createQuery(listQuery)
.setFirstResult(PageableUtils.getOffsetAsInteger(pageable))
.setMaxResults(pageable.getPageSize())
.getResultList();
}
Unfortunately it doesn’t work and I always get this error message:
org.springframework.orm.jpa.JpaSystemException: Could not locate TableGroup
Any hint would be appreciated. Thanks.