Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 315 Vote(s) - 3.47 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to sort by multiple properties in Spring Data (JPA) derived queries?

#1
I'm looking at the examples giving on this page (

[To see links please register here]

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

#2
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.
Reply

#3
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.
Reply

#4
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));
Reply

#5
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"))
);
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through