BAEL-6128 - New interfaces in Spring Data 3
This commit is contained in:
		
							parent
							
								
									639de9687e
								
							
						
					
					
						commit
						772a1ac80e
					
				
							
								
								
									
										6
									
								
								persistence-modules/spring-data-jpa-repo-3/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								persistence-modules/spring-data-jpa-repo-3/README.md
									
									
									
									
									
										Normal 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) | ||||
							
								
								
									
										35
									
								
								persistence-modules/spring-data-jpa-repo-3/pom.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								persistence-modules/spring-data-jpa-repo-3/pom.xml
									
									
									
									
									
										Normal 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> | ||||
| @ -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); | ||||
|     } | ||||
| } | ||||
| @ -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; | ||||
|     } | ||||
| } | ||||
| @ -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); | ||||
| } | ||||
| @ -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); | ||||
| } | ||||
| @ -0,0 +1 @@ | ||||
| 
 | ||||
| @ -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()); | ||||
|     } | ||||
| } | ||||
| @ -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()); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user