Good afternoon! Can anyone tell me how to use orderby in a subquery?
You just write order by
in the subquery, what’s the problem?
My question is how to do this. When I create a query, I can call orderby like this:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Book> cq = cb.createQuery(Book.class);
Root<Book> root = cq.from(Book.class);
cq.orderBy(cb.asc(root.get(Book_.title)));
But I can’t call orderby in the subquery:
Subquery sub = cq.subquery(Long.class);
sub.orderBy //This doesn't work
Because JPA does not support this. Hibernate ORM on the other hand does, so you will have to use Hibernate ORM interfaces.
For every JPA CriteriaBuilder API interface, there is usually an extension e.g. org.hibernate.query.criteria.JpaSubQuery
which contains all the extensions that Hibernate ORM offers. That type supports calling orderBy
.