Spring JPA PageRequest ordering by a join column

2020-04-04 07:03发布

I'm using the Spring PageRequest to sort (order) a custom query by a column in my database.

If I'm doing a custom query such as :

 @Query( value = "select h from hunterhouse h join h.queens q where q.name = 'Computer Science'") 

Is it not possible to sort by a column in q, the table I am joining to?

PageRequest request = new PageRequest(page, size, Sort.Direction.DESC, "q.region");

debug comes out as "order by h.q.region" which is incorrect, is it not possible to order by a join column?

2条回答
Viruses.
2楼-- · 2020-04-04 07:16

I had the same problem! This bug was fixed in Spring Data JPA 1.7.3 - just make sure you have a version higher or equal to 1.7.3! After that it will work with queens.region

Spring source: https://jira.spring.io/browse/DATAJPA-726

查看更多
小情绪 Triste *
3楼-- · 2020-04-04 07:35

You simply put the complete path you want to join on into the sort expression. So what you'd need to use is queens.region which will then be translated into h.queens.region and appended to the JPQL query you defined.

查看更多
登录 后发表回答