95

I'm looking at the examples giving on this page (https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.repositories) about method naming, is it possible to create a complex chain method name such as

findByProgrammeAndDirectorAndProgDateBetweenOrderByProgDateStartTimeAsc

In the example they give, they are only doing an OrderBy on one value. In the example above ProgDate and StartTime would be two separate values.

Andriy Budzinskyy
  • 1,971
  • 22
  • 28
PDStat
  • 5,513
  • 10
  • 51
  • 86

4 Answers4

209

The trick is to simply delimit the properties you want to sort by using the direction keywords Asc and Desc. So what you probably want in your query method is something like:

…OrderByProgDateAscStartTimeAsc

Note, how we conclude the first property definition by Asc and keep going with the next property.

Generally speaking, we recommend switching to @Query based queries, once method names exceed a certain length or complexity. The main reason being that it's awkward for clients to call these very long methods. With @Query you rather get the full power of the query language plus a reasonably sized method name that might be of higher level language to express the intent of the query.

Oliver Drotbohm
  • 80,157
  • 18
  • 225
  • 211
  • is there any way to order by `JoinColumn` id? Thanks – Laxman Nov 18 '17 at 14:20
  • @Laxman check this: https://stackoverflow.com/questions/28084064/spring-jpa-pagerequest-ordering-by-a-join-column – dchang Oct 23 '19 at 11:11
  • It doesn't work if there are no any conditions. So findByIdOrderByStartAscIdDesc works, but findAllOrderByStartAscIdDesc doesn't. – Selindek Sep 11 '20 at 15:18
  • Is it possible to combine `OrderBy` method keyword with `Sort` parameter in a `Pageable` object? – Conscript Apr 20 '21 at 04:51
12

I am Sharing one other approach code snippet for implementing get operation where performing sort operation ordered by multiple column

        List<Order> orders = new ArrayList<Order>();

        Order StartTimeOrder = new Order(Sort.Direction.DESC, "StartTime");
        orders.add(StartTimeOrder);
        Order progDateOrder = new Order(Sort.Direction.ASC, "ProgDate");
        orders.add(progDateOrder);
        return repository.findAll(Sort.by(orders));
abhinav kumar
  • 1,487
  • 1
  • 12
  • 20
8

Yes it's should be possible:

Try this:

findByProgrammeAndDirectorAndProgDateBetweenOrderByProgDateStartTimeAsc(String programme, String director, Date progStart, Date progEnd);

I have not tested the code, but according to things I've already done, it should work.

Paulo Fidalgo
  • 21,709
  • 7
  • 99
  • 115
  • Is that signature correct? I would think the Between covers the progStart and progEnd, but OrderByProgDateStartTimeAsc, would that need an additional two parameters? – PDStat Aug 19 '14 at 10:48
  • 1
    @PaulStatham I believe it's right. Between needs two Dates and the Order don't need any argument, since you are instructing to order by a value that is matched by the column, like in SQL ... ORDER BY ProgDateStartTime ASC – Paulo Fidalgo Aug 19 '14 at 13:40
2

A bit more compact :

return repository.findAll(
  Sort.by(List.of(
    new Order(Sort.Direction.DESC, "StartTime"),
    new Order(Sort.Direction.ASC, "ProgDate")
  ))
);

or

return repository.findAll(
  Sort
  .by(Direction.DESC, "StartTime")
    .and(Sort.by(Sort.Direction.ASC, "ProgDate"))
);
user16547619
  • 199
  • 1
  • 4