Spring Data JPA - Pageable sorting on joined table

Viewed 1832

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. 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.

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

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? My Repository implements the JpaRepository

Thanks a lot.

1 Answers

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.

Related