Order By Date ASC with Spring Data

JavaSqlSpringPostgresqlSpring Data

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

List<Foo> findAllByOrderByDateAsc();

Solution 2 - Java

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

Solution 3 - Java

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

Solution 4 - Java

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

}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaximeFView Question on Stackoverflow
Solution 1 - JavaJohnny LimView Answer on Stackoverflow
Solution 2 - JavaJB NizetView Answer on Stackoverflow
Solution 3 - JavaBizmarckView Answer on Stackoverflow
Solution 4 - JavaDSKView Answer on Stackoverflow