openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Kevin Sutter <kwsut...@gmail.com>
Subject Re: JPA Criteria API query - sorting by number of elements in collection
Date Thu, 09 Jan 2014 13:59:59 GMT
Glad to hear that you worked through the problem. Thanks for letting us
know your solution.

Kevin


On Thu, Jan 9, 2014 at 5:05 AM, wlad <vvozar@yahoo.com> wrote:

> Yes, it does work, but with that query I am getting that extra data in
> result
> - number of comments. And I need to get result in a form of list of Post
> objects (List<Post>).
>
> In the meantime I have found a way to build this query using JPA Criteria
> API:
>
>         CriteriaBuilder cb = em.getCriteriaBuilder();
>         CriteriaQuery<Post> cq = cb.createQuery(Post.class);
>         Root<Post> p = cq.from(Post.class);
>         cq.select(p);
>
> cq.orderBy(cb.desc(cb.size(p.<Collection<Comment>>get("comments"))));
>
>         Query q = em.createQuery(cq);
>         List<Post> resultList = (List<Post>) q.getResultList();
>
> Anyway, thanks for reply.
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/JPA-Criteria-API-query-sorting-by-number-of-elements-in-collection-tp7585829p7585832.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message