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

查看:23
本文介绍了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");

debug 显示为order by 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天全站免登陆