Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA - Order By : Sorting with Method Name vs Sort Class

I'm using Spring PagingAndSortingRepository and I have a very easy findAll query:

inventoryDao.findAll(new Sort(Sort.Direction.DESC,"startTime"))

It's working fine, I just was wondering: is it possible to replace the query above using Method Name generation instead? (Somethnig like: inventoryDao.findAllOrderByStartTime() )

like image 427
István Békési Avatar asked Feb 16 '26 12:02

István Békési


1 Answers

You have to use findAllByOrderBy...

As the Spring Data documentation says, the first by in the method name acts as delimiter to indicate the start of the actual criteria, in your case the sorting so it's necessary to add this in the method name before Order word.

like image 175
robert08 Avatar answered Feb 19 '26 01:02

robert08



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!