获取按日期排序的Spring Data上的最后一条记录 [英] Get last records ordered by date on Spring Data
问题描述
我正在尝试在Spring数据存储库中定义一个方法来获取按日期排序的表上的最后一条记录.这是我的实体:
I'm trying to define a method in a Spring Data repository to fetch the last records on a table ordered by date. This is my entity:
@Entity
public class News {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String title;
@Column(nullable = false)
private String text;
private Date publicationDate;
/* Getters and Setters */
}
这是我的存储库:
public interface NewsRepository extends JpaRepository<News, Long> {
List<News> findFirst5OrderByPublicationDateDesc();
}
如果我尝试使用启动项目,则会收到下一个错误:
If I try to use launch the project I get the next error:
起因:org.springframework.data.mapping.PropertyReferenceException:否找到日期类型的属性描述!遍历的路径:News.publicationDate.
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property desc found for type Date! Traversed path: News.publicationDate.
如果删除Desc,我会得到:
And if I remove the Desc I get this:
由以下原因引起:java.util.NoSuchElementException
Caused by: java.util.NoSuchElementException
我做错了什么?
推荐答案
结果证明该方法的签名不正确.正确的是:
Turns out that the signature of the method was incorrect. The right one is:
findFirst5ByOrderByPublicationDateDesc()
有些困惑,因为在官方样本中它们具有以下特征:
Is a little confusing because in the official samples they have this:
List<User> findTop10ByLastname(String lastname, Pageable pageable);
您会看到只有一个,通常是一个.
As you can see there is only one By there, the usual one.
这篇关于获取按日期排序的Spring Data上的最后一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!