Java 使用 Spring Data 按日期 ASC 排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19733464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:47:59  来源:igfitidea点击:

Order By Date ASC with Spring Data

javasqlspringpostgresqlspring-data

提问by MaximeF

I try to make an application with Spring-Data-JPA on a table in order by ASC but it gives me an error:

我尝试在表上按 ASC 的顺序使用 Spring-Data-JPA 创建一个应用程序,但它给了我一个错误:

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

采纳答案by Johnny Lim

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

尝试在“All”和“Order”之间添加“By”,如下所示:

List<Foo> findAllByOrderByDateAsc();

回答by JB Nizet

I don't think you can use findAll as a prefix.

我认为您不能使用 findAll 作为前缀。

Regarding the query, select *is not valid JPQL. It should be

关于查询,select *无效的 JPQL。它应该是

select foo from Foo foo order by foo.date desc

回答by Bizmarck

dateis 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

date是 SQL 中的保留字。foo_date例如,尝试将表属性更改为,并将您的查询重写为SELECT * FROM foo ORDER BY foo_date DESC

回答by DSK

Example :

例子 :

databaseDAO.findByUserNameOrderByCreatedDateDesc(username);

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

根据用户名和 sortby 创建日期列出用户。

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

public List<RecentlyView>  findByUserNameOrderByCreatedDateDesc(String username);


}