I have entity that has several fields:
public class Some {
private Foo foo;
private Bar bar;
}
I want to have code which is capable to sort by foo and sort by bar depending on the request. I know that there is tool that could be used like this:
Sort sort = Sort.by(Sort.Direction.DESC, "foo");
Pageable pageable = PageRequest.of(page, limit, sort);
But it seems like this approach is not perfect because field names are hardcoded and in the case that some field name is changed and developer forgot to update this sorting code everything will look okay during compilation / startup stage, but will fail during runtime.
Is that possible to have alternative solution without hardcoding and duplicating same method several times just to change jpql query ?