获取 Spring Data 上按日期排序的最后记录

2024-08-23Java开发问题
1

本文介绍了获取 Spring Data 上按日期排序的最后记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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 上按日期排序的最后记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13