Ordering a join fetched collection in JPA using JPQL/HQL

stuff22 picture stuff22 · May 5, 2011 · Viewed 11k times · Source

Given the below JPQL statement, how do I modify it so that the kittens in the resulting list are ordered by their age property?

SELECT c FROM Cat c left join fetch c.kittens WHERE c.id = :id

I've tried multiple approches but without any luck. This is esentially what I would like to do, but it doesn't work:

SELECT c FROM Cat c left join fetch c.kittens k WHERE c.id = :id ORDER BY k.age

Answer

bigZee77 picture bigZee77 · May 5, 2011

Hej,

I don't think this is possible when applied using queries. But as far as I remember, you can use this to add default ordering to your collection in the mapping:

@OrderBy("myColumName asc")