MaximeF
MaximeF

Reputation: 5403

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();

Upvotes: 69

Views: 112872

Answers (4)

DSK
DSK

Reputation: 512

Example :

databaseDAO.findByUserNameOrderByCreatedDateDesc(username);

to list out users based on username and sortby created date.

@Repository
public interface DatabaseDAO extends JpaRepository<User,Integer> {

public List<RecentlyView>  findByUserNameOrderByCreatedDateDesc(String username);


}

Upvotes: 6

Johnny Lim
Johnny Lim

Reputation: 5843

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

List<Foo> findAllByOrderByDateAsc();

Upvotes: 186

Bizmarck
Bizmarck

Reputation: 2698

date is reserved word in SQL. Try changing the table property to foo_date, for example and rewrite your query as SELECT * FROM foo ORDER BY foo_date DESC

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 692073

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

Upvotes: 11

Related Questions