Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order By Date ASC with Spring Data

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(); 
like image 232
MaximeF Avatar asked Nov 01 '13 18:11

MaximeF


People also ask

How do I sort by date in JPA repository?

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.

How do you get data in descending order in spring boot?

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.

What methods will sort the query results by name and population in ascending order?

The Sort object sorts the query results by name in ascending order.

How do you write orderBy in JPA?

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.


2 Answers

Try to add "By" between "All" and "Order" like this:

List<Foo> findAllByOrderByDateAsc(); 
like image 104
Johnny Lim Avatar answered Sep 22 '22 19:09

Johnny Lim


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 
like image 34
JB Nizet Avatar answered Sep 22 '22 19:09

JB Nizet