Spring JPA PageRequest通过连接列排序 [英] Spring JPA PageRequest ordering by a join column

查看:1560
本文介绍了Spring JPA PageRequest通过连接列排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Spring PageRequest通过数据库中的列对自定义查询进行排序(排序).

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'") 

是否无法按我要加入的表q中的一列进行排序?

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");

调试显示为按h.q.region排序",这是不正确的,是否无法通过连接列进行排序?

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

推荐答案

您只需将要加入的完整路径放入排序表达式中.因此,您需要使用的是queens.region,然后将其转换为h.queens.region并将其附加到您定义的JPQL查询中.

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.

这篇关于Spring JPA PageRequest通过连接列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆