From 322af68ae2093966427c4265440f8994d2416a6d Mon Sep 17 00:00:00 2001 From: Trevor Gowing Date: Sun, 25 Nov 2018 17:11:31 +0200 Subject: [PATCH] How to sort query results with Spring Data BAEL-2331 --- .../passenger/PassengerRepository.java | 4 +++ .../PassengerRepositoryIntegrationTest.java | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/PassengerRepository.java b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/PassengerRepository.java index 58c782f27a..2e4561d91b 100644 --- a/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/PassengerRepository.java +++ b/persistence-modules/spring-data-jpa/src/main/java/com/baeldung/passenger/PassengerRepository.java @@ -2,7 +2,11 @@ package com.baeldung.passenger; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + interface PassengerRepository extends JpaRepository, CustomPassengerRepository { Passenger findFirstByOrderBySeatNumberAsc(); + + List findByOrderBySeatNumberAsc(); } diff --git a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/passenger/PassengerRepositoryIntegrationTest.java b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/passenger/PassengerRepositoryIntegrationTest.java index 19366730c5..5ca2d21421 100644 --- a/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/passenger/PassengerRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-jpa/src/test/java/com/baeldung/passenger/PassengerRepositoryIntegrationTest.java @@ -14,6 +14,8 @@ import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import java.util.List; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; import static org.junit.Assert.assertEquals; @DataJpaTest @@ -66,4 +68,30 @@ public class PassengerRepositoryIntegrationTest { Passenger actual = page.getContent().get(0); assertEquals(expected, actual); } + + @Test + public void givenSeveralPassengersWhenOrderedBySeatNumberAscThenThePassengersReturnedInCorrectOrder() { + Passenger fred = Passenger.from("Fred", "Bloggs", 22); + Passenger ricki = Passenger.from("Ricki", "Bobbie", 36); + Passenger jill = Passenger.from("Jill", "Smith", 50); + Passenger siya = Passenger.from("Siya", "Kolisi", 85); + Passenger eve = Passenger.from("Eve", "Jackson", 95); + + List passengers = repository.findByOrderBySeatNumberAsc(); + + assertThat(passengers, contains(fred, ricki, jill, siya, eve)); + } + + @Test + public void givenSeveralPassengersWhenFindAllWithSortBySeatNumberAscThenReturnPassengersInCorrectOrder() { + Passenger fred = Passenger.from("Fred", "Bloggs", 22); + Passenger ricki = Passenger.from("Ricki", "Bobbie", 36); + Passenger jill = Passenger.from("Jill", "Smith", 50); + Passenger siya = Passenger.from("Siya", "Kolisi", 85); + Passenger eve = Passenger.from("Eve", "Jackson", 95); + + List passengers = repository.findAll(Sort.by(Sort.Direction.ASC, "seatNumber")); + + assertThat(passengers, contains(fred, ricki, jill, siya, eve)); + } }