I try to make an application with Spring-Data-JPA on a table in order by ASC but it gives me an error:
Invalid derived query! No property asc found for type java.util.Calendar
Why ?
List<Foo> findAllOrderByDateAsc();
or
@Query("SELECT * FROM foo ORDER BY date ASC") List<Foo> findAllOrderByDateAsc();
Data JPA provides Sorting support out of the box. To add Sorting support to our Repositories, we need to extend the PagingAndSortingRepository<T, ID> interface rather than the basic CrudRepository<T, ID> interface. List<Laptop> findAll(Sort sort); Returns a sorted list of laptops.
One option is to use Spring Data's method derivation, whereby the query is generated from the method name and signature. All we need to do here to sort our data is include the keyword OrderBy in our method name, along with the property name(s) and direction (Asc or Desc) by which we want to sort.
The Sort object sorts the query results by name in ascending order.
With JPA Criteria – the orderBy method is a “one stop” alternative to set all sorting parameters: both the order direction and the attributes to sort by can be set. Following is the method's API: orderBy(CriteriaBuilder. asc): Sorts in ascending order.
Try to add "By" between "All" and "Order" like this:
List<Foo> findAllByOrderByDateAsc();
I don't think you can use findAll as a prefix.
Regarding the query, select *
is not valid JPQL. It should be
select foo from Foo foo order by foo.date desc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With