简体   繁体   English

Spring Data JPA - 在连接表上的可分页排序

[英]Spring Data JPA - Pageable sorting on joined table

I am constructing a Pageable POJO by joining 2 tables that have a ManyToMany relationship and are related through a third table, a reference of which exists in the first table. 我正在构建一个Pageable POJO,它通过连接两个具有ManyToMany关系的表并通过第三个表相关联,第三个表的引用存在于第一个表中。 I will simplify my query to the core of the problem. 我将简化我的查询到问题的核心。

@Query("SELECT new package.myDTO(t1.name, t2.description) FROM table1 t1, table2 t2 where t1.relatedtable.table2Id = t2.id") Page<myDTO> findSomething(Pageable pageable);

All my fields have to be sortable but as I notice from the Hibernate result of this query, it always uses the first Entity provided in the Query (table1 in this case) to apply the sorting that comes from my Pageable object. 我的所有字段都必须是可排序的,但正如我从该查询的Hibernate结果中注意到的那样,它总是使用Query中提供的第一个Entity(在本例中为table1)来应用来自我的Pageable对象的排序。

So if I want to sort by the description of table2, passing "description" in my sort field results in order by t1.description instead of order by t2.description 因此,如果我想按表2的描述进行排序,则在我的排序字段中传递“description” order by t1.description排序order by t1.description而不是order by t2.description

Is there anything I am missing or any specific way to construct a Sort object by providing explicitly the table that should be used in order by ? 是否有任何我缺少的或通过明确提供应按order by使用的表来构造Sort对象的任何特定方法? My Repository implements the JpaRepository My Repository实现了JpaRepository

Thanks a lot. 非常感谢。

You could try using `` (use singular either side of table name but that does not render correctly here) to escape out the table name. 您可以尝试使用``(在表名的任何一侧使用奇异但在此处无法正确呈现)以逃避表名。 Escaping out the table name seemed to help Spring ignore it (not tack it on the front of the Sort field names) when I got a similar problem using subqueries. 当我使用子查询遇到类似的问题时,退出表名似乎有助于Spring忽略它(不会在Sort字段名称的前面添加它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM