Get last records ordered by date on Spring Data
Solution 1
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.
Solution 2
Spring JPaRepository has pagination which can be of great help. This will also work perfectly
To return the top 10 records, you can use:
Create a custom Pageable object
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
Page<News> findByPublicationDate(Date date, Pageable pageable);
This will return the top records.
To return the last 10 records, you can use:
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.
The advantage of using pages is that it becomes flexible to sort by another column. (Either ASC or 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
Comments
-
David Moreno García over 3 years
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 */ }
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.
And if I remove the Desc I get this:
Caused by: java.util.NoSuchElementException
What I'm doing wrong?
-
redoff about 5 yearsThat works good, but pay attention to Collections.reverse(MYLIST) which is read-only. You should make a copy of the list and then reverse it.
List<News> reversedNews = new ArrayList<>(bottomPage.getContent()); Collections.reverse(reversedNews);
-
Tatiana B over 4 yearsIs there a way to also introduce and id in such a query? ex:
findFirstByOrderByPublicationDateDescAndId(String id)
-
Aleksandr Baklanov almost 3 years@TatianaB, yes!
findFirstByIdOrderByPublicationDateDesc(String id)