From 841599a57974ed8a1a12fe5a6bdbe9e28c2f2666 Mon Sep 17 00:00:00 2001 From: Azhwani Date: Sun, 10 Dec 2023 18:54:05 +0100 Subject: [PATCH] BAEL-7280: How to get last record ordered by date in Spring Data --- .../jpa/listrepositories/lastrecord/Post.java | 40 +++++++++++++++++++ .../lastrecord/PostRepository.java | 15 +++++++ .../src/main/resources/data.sql | 8 +++- .../src/main/resources/schema.sql | 7 ++++ .../PostRepositoryIntegrationTest.java | 40 +++++++++++++++++++ 5 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/lastrecord/Post.java create mode 100644 persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/lastrecord/PostRepository.java create mode 100644 persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/listrepositories/lastrecord/PostRepositoryIntegrationTest.java diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/lastrecord/Post.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/lastrecord/Post.java new file mode 100644 index 0000000000..c797a6378f --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/lastrecord/Post.java @@ -0,0 +1,40 @@ +package com.baeldung.spring.data.jpa.listrepositories.lastrecord; + +import java.time.LocalDate; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +@Entity +public class Post { + + @Id + private Long id; + private String title; + private LocalDate publicationDate; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public LocalDate getPublicationDate() { + return publicationDate; + } + + public void setPublicationDate(LocalDate publicationDate) { + this.publicationDate = publicationDate; + } + +} diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/lastrecord/PostRepository.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/lastrecord/PostRepository.java new file mode 100644 index 0000000000..50d9e399c3 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/lastrecord/PostRepository.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.data.jpa.listrepositories.lastrecord; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +public interface PostRepository extends JpaRepository { + + Post findFirstByOrderByPublicationDateDesc(); + + Post findTopByOrderByPublicationDateDesc(); + + @Query("SELECT p FROM Post p ORDER BY p.publicationDate DESC LIMIT 1") + Post findLastPost(); + +} diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/resources/data.sql b/persistence-modules/spring-data-jpa-repo-3/src/main/resources/data.sql index 50bd326155..8f114f1aa5 100644 --- a/persistence-modules/spring-data-jpa-repo-3/src/main/resources/data.sql +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/resources/data.sql @@ -1,4 +1,10 @@ INSERT INTO person (id, first_name, last_name) VALUES(1, 'Azhrioun', 'Abderrahim'); INSERT INTO person (id, first_name, last_name) VALUES(2, 'Brian', 'Wheeler'); INSERT INTO person (id, first_name, last_name) VALUES(3, 'Stella', 'Anderson'); -INSERT INTO person (id, first_name, last_name) VALUES(4, 'Stella', 'Wheeler'); \ No newline at end of file +INSERT INTO person (id, first_name, last_name) VALUES(4, 'Stella', 'Wheeler'); + +INSERT INTO post (id, title, publication_date) VALUES(1, 'Facebook post', '2020-11-10'); +INSERT INTO post (id, title, publication_date) VALUES(2, 'Instagram post', '2020-12-24'); +INSERT INTO post (id, title, publication_date) VALUES(3, 'Twitter post', '2023-01-10'); +INSERT INTO post (id, title, publication_date) VALUES(4, 'tiktok post', '2023-03-18'); +INSERT INTO post (id, title, publication_date) VALUES(5, 'Pinterest post', '2023-09-09'); \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/resources/schema.sql b/persistence-modules/spring-data-jpa-repo-3/src/main/resources/schema.sql index c8be0eb781..c991c04900 100644 --- a/persistence-modules/spring-data-jpa-repo-3/src/main/resources/schema.sql +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/resources/schema.sql @@ -3,4 +3,11 @@ CREATE TABLE person( id INT PRIMARY KEY, first_name VARCHAR(200), last_name VARCHAR(200) +); + +DROP TABLE IF EXISTS post; +CREATE TABLE post( + id INT PRIMARY KEY, + title VARCHAR(200), + publication_date DATE ) \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/listrepositories/lastrecord/PostRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/listrepositories/lastrecord/PostRepositoryIntegrationTest.java new file mode 100644 index 0000000000..ecfb28574e --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/listrepositories/lastrecord/PostRepositoryIntegrationTest.java @@ -0,0 +1,40 @@ +package com.baeldung.spring.data.jpa.listrepositories.lastrecord; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; + +@DataJpaTest +class PostRepositoryIntegrationTest { + + @Autowired + private PostRepository postRepository; + + @Test + void givenPosts_whenUsingFindFirstDerivedQuery_thenReturnLastPost() { + Post post = postRepository.findFirstByOrderByPublicationDateDesc(); + + assertNotNull(post); + assertEquals(5, post.getId()); + } + + @Test + void givenPosts_whenUsingFindTopDerivedQuery_thenReturnLastPost() { + Post post = postRepository.findTopByOrderByPublicationDateDesc(); + + assertNotNull(post); + assertEquals(5, post.getId()); + } + + @Test + void givenPosts_whenUsingQueryAnnotation_thenReturnLastPost() { + Post post = postRepository.findLastPost(); + + assertNotNull(post); + assertEquals(5, post.getId()); + } + +}