Merge pull request #13541 from abh1navv/BAEL-6128

BAEL-6128 - New interfaces in Spring Data 3
This commit is contained in:
davidmartinezbarua 2023-03-12 20:48:43 -03:00 committed by GitHub
commit f0f9e25e83
10 changed files with 206 additions and 2 deletions

View File

@ -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)

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-3</relativePath>
</parent>
<artifactId>spring-data-jpa-repo-3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-data-jpa-repo-3</name>
<description>spring-data-jpa-repo-3</description>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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<Book, Long> {
List<Book> findBooksByAuthor(String author);
}

View File

@ -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<Book, Long>, ListCrudRepository<Book, Long> {
List<Book> findBooksByAuthor(String author, Pageable pageable);
}

View File

@ -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<Book> books = bookListRepository.findBooksByAuthor("John Doe");
Assertions.assertEquals(3, books.size());
}
}

View File

@ -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<Book> 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());
}
}

View File

@ -833,7 +833,7 @@
<module>di-modules</module>
<module>asciidoctor</module>
<module>aws-modules</module>
<module>couchbase</module>
<module>core-groovy-modules</module>
@ -893,6 +893,7 @@
<module>optaplanner</module>
<module>persistence-modules/sirix</module>
<module>persistence-modules/spring-data-cassandra-2</module>
<module>persistence-modules/spring-data-jpa-repo-3</module>
<module>quarkus-modules</module>
<module>spring-boot-modules/spring-boot-cassandre</module>
<module>spring-boot-modules/spring-boot-3</module>
@ -1128,7 +1129,7 @@
<module>core-java-modules/core-java-strings</module>
<module>core-java-modules/core-java-httpclient</module>
<module>spring-aop</module>
<module>spring-aop-2</module>
<module>spring-aop-2</module>
<module>custom-pmd</module>
<module>spring-core-6</module>
<module>data-structures</module>
@ -1149,6 +1150,7 @@
<module>optaplanner</module>
<module>persistence-modules/sirix</module>
<module>persistence-modules/spring-data-cassandra-2</module>
<module>persistence-modules/spring-data-jpa-repo-3</module>
<module>quarkus-modules</module>
<module>spring-boot-modules/spring-boot-cassandre</module>
<module>spring-boot-modules/spring-boot-3</module>