Skip to content

Commit

Permalink
fix: create count criteria query. Remove order-by from sql (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras authored Aug 8, 2023
1 parent f53e5d2 commit 8e84221
Showing 1 changed file with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@

import jakarta.persistence.EntityManager;
import jakarta.persistence.NoResultException;
import jakarta.persistence.TypedQuery;
import jakarta.persistence.criteria.*;

import org.hibernate.query.sqm.internal.QuerySqmImpl;
import org.hibernate.query.sqm.tree.SqmCopyContext;
import org.hibernate.query.sqm.tree.select.SqmQueryPart;
import org.hibernate.query.sqm.tree.select.SqmSelectStatement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -68,16 +67,13 @@ public PageResult<T> getPageResult() {
// ignore
}

TypedQuery<T> tmnp = em.createQuery(criteria);
QuerySqmImpl<?> tmp = tmnp.unwrap(QuerySqmImpl.class);

// get stream
Stream<T> stream = em.createQuery(criteria)
.setFirstResult(page.number() * page.size())
.setMaxResults(page.size())
.getResultStream();
// create page result
return new PageResult<T>(count, stream, page);
return new PageResult<>(count, stream, page);
} catch (Exception ex) {
String entityClass = criteria.getResultType() != null ? criteria.getResultType().getName() : null;
throw new DAOException(Errors.GET_PAGE_RESULT_ERROR, ex, page.number(), page.size(), entityClass);
Expand Down Expand Up @@ -181,7 +177,7 @@ public static <T> CriteriaQuery<Long> createCountCriteria(EntityManager em, Crit
} else {
countExpression = builder.count(root);
}
return countCriteria.select(countExpression).groupBy(root);
return countCriteria.select(countExpression);
}

/**
Expand All @@ -196,7 +192,11 @@ public static <T> CriteriaQuery<Long> createCountCriteriaQuery(CriteriaBuilder b
CriteriaQuery<Long> result = builder.createQuery(Long.class);
SqmSelectStatement<T> copy = ((SqmSelectStatement<T>) from).copy(SqmCopyContext.simpleContext());
SqmSelectStatement<T> r = (SqmSelectStatement<T>) result;
r.setQueryPart(copy.getQueryPart());
SqmQueryPart<T> part = copy.getQueryPart();
// remove group by from the count
part.setOrderByClause(null);
r.setQueryPart(part);
r.getOrderList().clear();
return result;
}

Expand Down

0 comments on commit 8e84221

Please sign in to comment.