1

I have many tables with sortable headers in the frontend. When any of them is clicked, a request similar to

http://localhost:8080/api/v1/items/?page=0&size=10&sort=item.type.name,asc&itemId=4

is sent to the backend where it gets automatically mapped to a Pageable object and goes to a Repository.
Notice the item.type.name part. If relationships are relatively simple, this works but does not feel like a good way to do it.

What if I need to compare against a property of an object which is inside a collection?

What is the best practice to sort by values from lists like errors in the item.order.errors.name example, when the relationship is one to many?

I'm afraid writing sorting headers like item.type.name or even a.b.c.d.e.f.g in a bad scenario, creates a very tight coupling where it will be difficult to change or refactor code in the future without breaking anything.

lisymcaydnlb
  • 150
  • 2
  • 12
  • 1
    I don't know the answer to this question, but I do see that the Pageable's [Sort object](https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Sort.html) is something that can be subclassed. Maybe there's a way there, or even an existing subclass that would help. I can't tell from a quick glance. The [TypedSort](https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Sort.TypedSort.html) maybe? – xdhmoore Oct 26 '20 at 06:09
  • 1
    If you need to refactor the backend, you need to frontend also. If you don't want to then just send the param `sortParam=itemTypeName` and manage in the backend when needed and create pageable/sort object manually. – Eklavya Oct 26 '20 at 07:32

0 Answers0