Java 获取按 Spring Data 上的数据排序的最后记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27567351/
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-11 04:36:45  来源:igfitidea点击:

Get last records ordered by data on Spring Data

javaspringspring-mvcspring-data

提问by David Moreno García

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:

我正在尝试在 Spring Data 存储库中定义一个方法来获取按日期排序的表上的最后一条记录。这是我的实体:

@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 */
}

And this is my repository:

这是我的存储库:

public interface NewsRepository extends JpaRepository<News, Long> {
    List<News> findFirst5OrderByPublicationDateDesc();
}

If I try to use launch the project I get the next error:

如果我尝试使用启动项目,我会收到下一个错误:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property desc found for type Date! Traversed path: News.publicationDate.

引起:org.springframework.data.mapping.PropertyReferenceException:没有找到日期类型的属性描述!遍历路径:News.publicationDate。

And if I remove the Desc I get this:

如果我删除 Desc 我得到这个:

Caused by: java.util.NoSuchElementException

引起:java.util.NoSuchElementException

What I'm doing wrong?

我做错了什么?

采纳答案by David Moreno García

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.

如您所见,只有一个 By there,通常的一个。

回答by Peter Mutisya

Spring JPaRepository has pagination which can be of great help. This will also work perfectly

Spring JPaRepository 具有分页功能,可以提供很大帮助。这也将完美地工作

To return the top 10 records, you can use:

要返回前 10 条记录,您可以使用:

Create a custom Pageable object

创建自定义 Pageable 对象

Pageable pageable = new PageRequest(0, 10, Sort.Direction.ASC, "id");

Page<News> topPage = newsRepository.findByPublicationDate(id, pageable);
List<News> topUsersList = topPage.getContent();

In the NewsRepository interface, be sure to create a method

在 NewsRepository 接口中,一定要创建一个方法

 Page<News> findByPublicationDate(Date date, Pageable pageable);

This will return the top records.

这将返回顶部记录。

To return the last 10 records, you can use:

要返回最后 10 条记录,您可以使用:

Pageable pageable = new PageRequest(0, 10, Sort.Direction.DESC, "id");

Page<News> bottomPage = newsRepository.findByPublicationDate(id, pageable);
// this is a list of the last 10 records, you can choose to invert it by using
List<News> bottomUsersList = bottomPage.getContent();

Collections.inverse(bottomUsersList);

This will reuse the same NewsRespoitory, so no need to create another method there.

这将重用相同的 NewsRespoitory,因此无需在那里创建另一个方法。

The advantage of using pages is that it becomes flexible to sort by another column. (Either ASC or DESC)

使用页面的优点是可以灵活地按另一列排序。(ASC 或 DESC)

// To get top by text
Pageable pageable = new PageRequest(0, 10, Sort.Direction.ASC, "text");
// Top by title
Pageable pageable = new PageRequest(0, 10, Sort.Direction.ASC, "title");
// Top by publicationDate
Pageable pageable = new PageRequest(0, 10, Sort.Direction.ASC, "publicationDate");

10 can be replaced by whatever number of records you need returned

10 可以替换为您需要返回的任意数量的记录