Ordering a join fetched collection in JPA using JPQL/HQL

后端 未结 3 1887
被撕碎了的回忆
被撕碎了的回忆 2021-02-07 14:40

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          


        
3条回答
  •  被撕碎了的回忆
    2021-02-07 15:36

    In addition to @bigZee77's answer, you could also perhaps change your query and query for the kitten instead of the cat. The resulting list of kittens would be ordered, and every kitten would point to the same cat:

    select k from Cat c inner join fetch c.kittens k where c.id = :id order by k.age
    

    If the cat doesn't have any kitten, you would get an empty list, though.

    The alternative is of course to provide a Java method which sorts the list of kittens.

提交回复
热议问题