Merge pull request #15391 from azhwani/BAEL-7280
BAEL-7280: How to get last record ordered by date in Spring Data
This commit is contained in:
commit
af56780cfa
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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, Integer> {
|
||||
|
||||
Post findFirstByOrderByPublicationDateDesc();
|
||||
|
||||
Post findTopByOrderByPublicationDateDesc();
|
||||
|
||||
@Query("SELECT p FROM Post p ORDER BY p.publicationDate DESC LIMIT 1")
|
||||
Post findLastPost();
|
||||
|
||||
}
|
|
@ -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');
|
||||
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');
|
|
@ -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
|
||||
)
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue