[BAEL-17317] - Check article code - https://www.baeldung.com/spring-data-jpa-pagination-sorting
This commit is contained in:
parent
d47ea19834
commit
916aae0d91
|
@ -5,9 +5,9 @@ import java.util.List;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||||
|
|
||||||
import com.baeldung.multipledb.model.product.ProductMultipleDB;
|
import com.baeldung.multipledb.model.product.Product;
|
||||||
|
|
||||||
public interface ProductRepository extends PagingAndSortingRepository<ProductMultipleDB, Integer> {
|
public interface ProductRepository extends PagingAndSortingRepository<Product, Integer> {
|
||||||
|
|
||||||
List<ProductMultipleDB> findAllByPrice(double price, Pageable pageable);
|
List<Product> findAllByPrice(double price, Pageable pageable);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import javax.persistence.Table;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(schema = "products")
|
@Table(schema = "products")
|
||||||
public class ProductMultipleDB {
|
public class Product {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
private int id;
|
private int id;
|
||||||
|
@ -15,19 +15,19 @@ public class ProductMultipleDB {
|
||||||
|
|
||||||
private double price;
|
private double price;
|
||||||
|
|
||||||
public ProductMultipleDB() {
|
public Product() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ProductMultipleDB(int id, String name, double price) {
|
private Product(int id, String name, double price) {
|
||||||
super();
|
super();
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.price = price;
|
this.price = price;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ProductMultipleDB from(int id, String name, double price) {
|
public static Product from(int id, String name, double price) {
|
||||||
return new ProductMultipleDB(id, name, price);
|
return new Product(id, name, price);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getId() {
|
public int getId() {
|
|
@ -19,7 +19,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||||
import com.baeldung.multipledb.dao.product.ProductRepository;
|
import com.baeldung.multipledb.dao.product.ProductRepository;
|
||||||
import com.baeldung.multipledb.dao.user.PossessionRepository;
|
import com.baeldung.multipledb.dao.user.PossessionRepository;
|
||||||
import com.baeldung.multipledb.dao.user.UserRepository;
|
import com.baeldung.multipledb.dao.user.UserRepository;
|
||||||
import com.baeldung.multipledb.model.product.ProductMultipleDB;
|
import com.baeldung.multipledb.model.product.Product;
|
||||||
import com.baeldung.multipledb.model.user.PossessionMultipleDB;
|
import com.baeldung.multipledb.model.user.PossessionMultipleDB;
|
||||||
import com.baeldung.multipledb.model.user.UserMultipleDB;
|
import com.baeldung.multipledb.model.user.UserMultipleDB;
|
||||||
|
|
||||||
|
@ -84,7 +84,7 @@ public class JpaMultipleDBIntegrationTest {
|
||||||
@Test
|
@Test
|
||||||
@Transactional("productTransactionManager")
|
@Transactional("productTransactionManager")
|
||||||
public void whenCreatingProduct_thenCreated() {
|
public void whenCreatingProduct_thenCreated() {
|
||||||
ProductMultipleDB product = new ProductMultipleDB();
|
Product product = new Product();
|
||||||
product.setName("Book");
|
product.setName("Book");
|
||||||
product.setId(2);
|
product.setId(2);
|
||||||
product.setPrice(20);
|
product.setPrice(20);
|
||||||
|
|
|
@ -25,7 +25,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import com.baeldung.multipledb.PersistenceProductConfiguration;
|
import com.baeldung.multipledb.PersistenceProductConfiguration;
|
||||||
import com.baeldung.multipledb.dao.product.ProductRepository;
|
import com.baeldung.multipledb.dao.product.ProductRepository;
|
||||||
import com.baeldung.multipledb.model.product.ProductMultipleDB;
|
import com.baeldung.multipledb.model.product.Product;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest(classes=MultipleDbApplication.class)
|
@SpringBootTest(classes=MultipleDbApplication.class)
|
||||||
|
@ -38,22 +38,22 @@ public class ProductRepositoryIntegrationTest {
|
||||||
@Before
|
@Before
|
||||||
@Transactional("productTransactionManager")
|
@Transactional("productTransactionManager")
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
productRepository.save(ProductMultipleDB.from(1001, "Book", 21));
|
productRepository.save(Product.from(1001, "Book", 21));
|
||||||
productRepository.save(ProductMultipleDB.from(1002, "Coffee", 10));
|
productRepository.save(Product.from(1002, "Coffee", 10));
|
||||||
productRepository.save(ProductMultipleDB.from(1003, "Jeans", 30));
|
productRepository.save(Product.from(1003, "Jeans", 30));
|
||||||
productRepository.save(ProductMultipleDB.from(1004, "Shirt", 32));
|
productRepository.save(Product.from(1004, "Shirt", 32));
|
||||||
productRepository.save(ProductMultipleDB.from(1005, "Bacon", 10));
|
productRepository.save(Product.from(1005, "Bacon", 10));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void whenRequestingFirstPageOfSizeTwo_ThenReturnFirstPage() {
|
public void whenRequestingFirstPageOfSizeTwo_ThenReturnFirstPage() {
|
||||||
Pageable pageRequest = PageRequest.of(0, 2);
|
Pageable pageRequest = PageRequest.of(0, 2);
|
||||||
|
|
||||||
Page<ProductMultipleDB> result = productRepository.findAll(pageRequest);
|
Page<Product> result = productRepository.findAll(pageRequest);
|
||||||
|
|
||||||
assertThat(result.getContent(), hasSize(2));
|
assertThat(result.getContent(), hasSize(2));
|
||||||
assertTrue(result.stream()
|
assertTrue(result.stream()
|
||||||
.map(ProductMultipleDB::getId)
|
.map(Product::getId)
|
||||||
.allMatch(id -> Arrays.asList(1001, 1002)
|
.allMatch(id -> Arrays.asList(1001, 1002)
|
||||||
.contains(id)));
|
.contains(id)));
|
||||||
}
|
}
|
||||||
|
@ -62,11 +62,11 @@ public class ProductRepositoryIntegrationTest {
|
||||||
public void whenRequestingSecondPageOfSizeTwo_ThenReturnSecondPage() {
|
public void whenRequestingSecondPageOfSizeTwo_ThenReturnSecondPage() {
|
||||||
Pageable pageRequest = PageRequest.of(1, 2);
|
Pageable pageRequest = PageRequest.of(1, 2);
|
||||||
|
|
||||||
Page<ProductMultipleDB> result = productRepository.findAll(pageRequest);
|
Page<Product> result = productRepository.findAll(pageRequest);
|
||||||
|
|
||||||
assertThat(result.getContent(), hasSize(2));
|
assertThat(result.getContent(), hasSize(2));
|
||||||
assertTrue(result.stream()
|
assertTrue(result.stream()
|
||||||
.map(ProductMultipleDB::getId)
|
.map(Product::getId)
|
||||||
.allMatch(id -> Arrays.asList(1003, 1004)
|
.allMatch(id -> Arrays.asList(1003, 1004)
|
||||||
.contains(id)));
|
.contains(id)));
|
||||||
}
|
}
|
||||||
|
@ -75,11 +75,11 @@ public class ProductRepositoryIntegrationTest {
|
||||||
public void whenRequestingLastPage_ThenReturnLastPageWithRemData() {
|
public void whenRequestingLastPage_ThenReturnLastPageWithRemData() {
|
||||||
Pageable pageRequest = PageRequest.of(2, 2);
|
Pageable pageRequest = PageRequest.of(2, 2);
|
||||||
|
|
||||||
Page<ProductMultipleDB> result = productRepository.findAll(pageRequest);
|
Page<Product> result = productRepository.findAll(pageRequest);
|
||||||
|
|
||||||
assertThat(result.getContent(), hasSize(1));
|
assertThat(result.getContent(), hasSize(1));
|
||||||
assertTrue(result.stream()
|
assertTrue(result.stream()
|
||||||
.map(ProductMultipleDB::getId)
|
.map(Product::getId)
|
||||||
.allMatch(id -> Arrays.asList(1005)
|
.allMatch(id -> Arrays.asList(1005)
|
||||||
.contains(id)));
|
.contains(id)));
|
||||||
}
|
}
|
||||||
|
@ -88,12 +88,12 @@ public class ProductRepositoryIntegrationTest {
|
||||||
public void whenSortingByNameAscAndPaging_ThenReturnSortedPagedResult() {
|
public void whenSortingByNameAscAndPaging_ThenReturnSortedPagedResult() {
|
||||||
Pageable pageRequest = PageRequest.of(0, 3, Sort.by("name"));
|
Pageable pageRequest = PageRequest.of(0, 3, Sort.by("name"));
|
||||||
|
|
||||||
Page<ProductMultipleDB> result = productRepository.findAll(pageRequest);
|
Page<Product> result = productRepository.findAll(pageRequest);
|
||||||
|
|
||||||
assertThat(result.getContent(), hasSize(3));
|
assertThat(result.getContent(), hasSize(3));
|
||||||
assertThat(result.getContent()
|
assertThat(result.getContent()
|
||||||
.stream()
|
.stream()
|
||||||
.map(ProductMultipleDB::getId)
|
.map(Product::getId)
|
||||||
.collect(Collectors.toList()), equalTo(Arrays.asList(1005, 1001, 1002)));
|
.collect(Collectors.toList()), equalTo(Arrays.asList(1005, 1001, 1002)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -103,12 +103,12 @@ public class ProductRepositoryIntegrationTest {
|
||||||
Pageable pageRequest = PageRequest.of(0, 3, Sort.by("price")
|
Pageable pageRequest = PageRequest.of(0, 3, Sort.by("price")
|
||||||
.descending());
|
.descending());
|
||||||
|
|
||||||
Page<ProductMultipleDB> result = productRepository.findAll(pageRequest);
|
Page<Product> result = productRepository.findAll(pageRequest);
|
||||||
|
|
||||||
assertThat(result.getContent(), hasSize(3));
|
assertThat(result.getContent(), hasSize(3));
|
||||||
assertThat(result.getContent()
|
assertThat(result.getContent()
|
||||||
.stream()
|
.stream()
|
||||||
.map(ProductMultipleDB::getId)
|
.map(Product::getId)
|
||||||
.collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001)));
|
.collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -119,12 +119,12 @@ public class ProductRepositoryIntegrationTest {
|
||||||
.descending()
|
.descending()
|
||||||
.and(Sort.by("name")));
|
.and(Sort.by("name")));
|
||||||
|
|
||||||
Page<ProductMultipleDB> result = productRepository.findAll(pageRequest);
|
Page<Product> result = productRepository.findAll(pageRequest);
|
||||||
|
|
||||||
assertThat(result.getContent(), hasSize(5));
|
assertThat(result.getContent(), hasSize(5));
|
||||||
assertThat(result.getContent()
|
assertThat(result.getContent()
|
||||||
.stream()
|
.stream()
|
||||||
.map(ProductMultipleDB::getId)
|
.map(Product::getId)
|
||||||
.collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001, 1005, 1002)));
|
.collect(Collectors.toList()), equalTo(Arrays.asList(1004, 1003, 1001, 1005, 1002)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -133,11 +133,11 @@ public class ProductRepositoryIntegrationTest {
|
||||||
public void whenRequestingFirstPageOfSizeTwoUsingCustomMethod_ThenReturnFirstPage() {
|
public void whenRequestingFirstPageOfSizeTwoUsingCustomMethod_ThenReturnFirstPage() {
|
||||||
Pageable pageRequest = PageRequest.of(0, 2);
|
Pageable pageRequest = PageRequest.of(0, 2);
|
||||||
|
|
||||||
List<ProductMultipleDB> result = productRepository.findAllByPrice(10, pageRequest);
|
List<Product> result = productRepository.findAllByPrice(10, pageRequest);
|
||||||
|
|
||||||
assertThat(result, hasSize(2));
|
assertThat(result, hasSize(2));
|
||||||
assertTrue(result.stream()
|
assertTrue(result.stream()
|
||||||
.map(ProductMultipleDB::getId)
|
.map(Product::getId)
|
||||||
.allMatch(id -> Arrays.asList(1002, 1005)
|
.allMatch(id -> Arrays.asList(1002, 1005)
|
||||||
.contains(id)));
|
.contains(id)));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue