diff --git a/persistence-modules/spring-data-jpa-repo-3/README.md b/persistence-modules/spring-data-jpa-repo-3/README.md new file mode 100644 index 0000000000..419394d459 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/README.md @@ -0,0 +1,6 @@ +## Spring Data JPA - Repositories + +This module contains articles about Spring Data JPA. + +### Relevant Articles: +- More articles: [[<-- prev]](../spring-data-jpa-repo-2) diff --git a/persistence-modules/spring-data-jpa-repo-3/pom.xml b/persistence-modules/spring-data-jpa-repo-3/pom.xml new file mode 100644 index 0000000000..8cd8ca7f61 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + + com.baeldung + parent-boot-3 + 0.0.1-SNAPSHOT + ../../parent-boot-3 + + spring-data-jpa-repo-3 + 0.0.1-SNAPSHOT + spring-data-jpa-repo-3 + spring-data-jpa-repo-3 + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + com.h2database + h2 + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + + + diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/Application.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/Application.java new file mode 100644 index 0000000000..13d9dbf414 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/Application.java @@ -0,0 +1,12 @@ +package com.baeldung.spring.data.jpa.listrepositories; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(SpringBootApplication.class, args); + } +} diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/entity/Book.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/entity/Book.java new file mode 100644 index 0000000000..40f9f689a6 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/entity/Book.java @@ -0,0 +1,57 @@ +package com.baeldung.spring.data.jpa.listrepositories.entity; + +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; + +@Entity +public class Book { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String title; + private String author; + private String isbn; + + public Book() { + } + + public Book(String title, String author, String isbn) { + this.title = title; + this.author = author; + this.isbn = isbn; + } + + 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 String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getIsbn() { + return isbn; + } + + public void setIsbn(String isbn) { + this.isbn = isbn; + } +} diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/repository/BookListRepository.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/repository/BookListRepository.java new file mode 100644 index 0000000000..aec8347850 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/repository/BookListRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.spring.data.jpa.listrepositories.repository; + +import com.baeldung.spring.data.jpa.listrepositories.entity.Book; +import org.springframework.data.repository.ListCrudRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface BookListRepository extends ListCrudRepository { + + List findBooksByAuthor(String author); +} diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/repository/BookPagingAndSortingRepository.java b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/repository/BookPagingAndSortingRepository.java new file mode 100644 index 0000000000..1b37b0d1ef --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/java/com/baeldung/spring/data/jpa/listrepositories/repository/BookPagingAndSortingRepository.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.data.jpa.listrepositories.repository; + +import com.baeldung.spring.data.jpa.listrepositories.entity.Book; +import org.springframework.data.domain.Pageable; +import org.springframework.data.repository.ListCrudRepository; +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public interface BookPagingAndSortingRepository extends PagingAndSortingRepository, ListCrudRepository { + + List findBooksByAuthor(String author, Pageable pageable); +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-repo-3/src/main/resources/application.properties b/persistence-modules/spring-data-jpa-repo-3/src/main/resources/application.properties new file mode 100644 index 0000000000..8b13789179 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/main/resources/application.properties @@ -0,0 +1 @@ + diff --git a/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/listrepositories/repository/BookListRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/listrepositories/repository/BookListRepositoryIntegrationTest.java new file mode 100644 index 0000000000..d004d485e4 --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/listrepositories/repository/BookListRepositoryIntegrationTest.java @@ -0,0 +1,28 @@ +package com.baeldung.spring.data.jpa.listrepositories.repository; + +import com.baeldung.spring.data.jpa.listrepositories.entity.Book; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.Arrays; +import java.util.List; + +@SpringBootTest +public class BookListRepositoryIntegrationTest { + + @Autowired + private BookListRepository bookListRepository; + + @Test + public void givenDbContainsBooks_whenFindBooksByAuthor_thenReturnBooksByAuthor() { + Book book1 = new Book("Spring Data", "John Doe", "1234567890"); + Book book2 = new Book("Spring Data 2", "John Doe", "1234567891"); + Book book3 = new Book("Spring Data 3", "John Doe", "1234567892"); + bookListRepository.saveAll(Arrays.asList(book1, book2, book3)); + + List books = bookListRepository.findBooksByAuthor("John Doe"); + Assertions.assertEquals(3, books.size()); + } +} \ 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/repository/BookPagingAndSortingRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/listrepositories/repository/BookPagingAndSortingRepositoryIntegrationTest.java new file mode 100644 index 0000000000..8f34e43e3f --- /dev/null +++ b/persistence-modules/spring-data-jpa-repo-3/src/test/java/com/baeldung/spring/data/jpa/listrepositories/repository/BookPagingAndSortingRepositoryIntegrationTest.java @@ -0,0 +1,35 @@ +package com.baeldung.spring.data.jpa.listrepositories.repository; + +import com.baeldung.spring.data.jpa.listrepositories.entity.Book; +import com.baeldung.spring.data.jpa.listrepositories.repository.BookPagingAndSortingRepository; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; + +import java.util.Arrays; +import java.util.List; + +@SpringBootTest +public class BookPagingAndSortingRepositoryIntegrationTest { + + @Autowired + private BookPagingAndSortingRepository bookPagingAndSortingRepository; + + @Test + public void givenDbContainsBooks_whenfindBooksByAuthor_thenReturnBooksByAuthor() { + Book book1 = new Book("Spring Data", "John Doe", "1234567890"); + Book book2 = new Book("Spring Data 2", "John Doe", "1234567891"); + Book book3 = new Book("Spring Data 3", "John Doe", "1234567892"); + bookPagingAndSortingRepository.saveAll(Arrays.asList(book1, book2, book3)); + + Pageable pageable = PageRequest.of(0, 2, Sort.by("title").descending()); + List books = bookPagingAndSortingRepository.findBooksByAuthor("John Doe", pageable); + Assertions.assertEquals(2, books.size()); + Assertions.assertEquals(book3.getId(), books.get(0).getId()); + Assertions.assertEquals(book2.getId(), books.get(1).getId()); + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 9b31ae9750..cf08bb08b2 100644 --- a/pom.xml +++ b/pom.xml @@ -833,7 +833,7 @@ di-modules asciidoctor aws-modules - + couchbase core-groovy-modules @@ -893,6 +893,7 @@ optaplanner persistence-modules/sirix persistence-modules/spring-data-cassandra-2 + persistence-modules/spring-data-jpa-repo-3 quarkus-modules spring-boot-modules/spring-boot-cassandre spring-boot-modules/spring-boot-3 @@ -1128,7 +1129,7 @@ core-java-modules/core-java-strings core-java-modules/core-java-httpclient spring-aop - spring-aop-2 + spring-aop-2 custom-pmd spring-core-6 data-structures @@ -1149,6 +1150,7 @@ optaplanner persistence-modules/sirix persistence-modules/spring-data-cassandra-2 + persistence-modules/spring-data-jpa-repo-3 quarkus-modules spring-boot-modules/spring-boot-cassandre spring-boot-modules/spring-boot-3