Merge pull request #5773 from trevorgowing/BAEL-2331-spring-data-sorting
BAEL-2331 Spring Data Sorting
This commit is contained in:
commit
15391f69ec
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.limit;
|
||||
package com.baeldung.passenger;
|
||||
|
||||
import java.util.List;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.limit;
|
||||
package com.baeldung.passenger;
|
||||
|
||||
import javax.persistence.Basic;
|
||||
import javax.persistence.Column;
|
||||
|
@ -17,7 +17,7 @@ class Passenger {
|
|||
|
||||
@Basic(optional = false)
|
||||
@Column(nullable = false)
|
||||
private String fistName;
|
||||
private String firstName;
|
||||
|
||||
@Basic(optional = false)
|
||||
@Column(nullable = false)
|
||||
|
@ -27,8 +27,8 @@ class Passenger {
|
|||
@Column(nullable = false)
|
||||
private int seatNumber;
|
||||
|
||||
private Passenger(String fistName, String lastName, int seatNumber) {
|
||||
this.fistName = fistName;
|
||||
private Passenger(String firstName, String lastName, int seatNumber) {
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
this.seatNumber = seatNumber;
|
||||
}
|
||||
|
@ -44,20 +44,20 @@ class Passenger {
|
|||
if (object == null || getClass() != object.getClass())
|
||||
return false;
|
||||
Passenger passenger = (Passenger) object;
|
||||
return getSeatNumber() == passenger.getSeatNumber() && Objects.equals(getFistName(), passenger.getFistName())
|
||||
return getSeatNumber() == passenger.getSeatNumber() && Objects.equals(getFirstName(), passenger.getFirstName())
|
||||
&& Objects.equals(getLastName(), passenger.getLastName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getFistName(), getLastName(), getSeatNumber());
|
||||
return Objects.hash(getFirstName(), getLastName(), getSeatNumber());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder toStringBuilder = new StringBuilder(getClass().getSimpleName());
|
||||
toStringBuilder.append("{ id=").append(id);
|
||||
toStringBuilder.append(", fistName='").append(fistName).append('\'');
|
||||
toStringBuilder.append(", firstName='").append(firstName).append('\'');
|
||||
toStringBuilder.append(", lastName='").append(lastName).append('\'');
|
||||
toStringBuilder.append(", seatNumber=").append(seatNumber);
|
||||
toStringBuilder.append('}');
|
||||
|
@ -68,8 +68,8 @@ class Passenger {
|
|||
return id;
|
||||
}
|
||||
|
||||
String getFistName() {
|
||||
return fistName;
|
||||
String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
String getLastName() {
|
|
@ -1,8 +1,12 @@
|
|||
package com.baeldung.limit;
|
||||
package com.baeldung.passenger;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
interface PassengerRepository extends JpaRepository<Passenger, Long>, CustomPassengerRepository {
|
||||
|
||||
Passenger findFirstByOrderBySeatNumberAsc();
|
||||
|
||||
List<Passenger> findByOrderBySeatNumberAsc();
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.limit;
|
||||
package com.baeldung.passenger;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.limit;
|
||||
package com.baeldung.passenger;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
@ -14,11 +14,13 @@ 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
|
||||
@RunWith(SpringRunner.class)
|
||||
public class LimitIntegrationTest {
|
||||
public class PassengerRepositoryIntegrationTest {
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
@ -66,4 +68,30 @@ public class LimitIntegrationTest {
|
|||
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<Passenger> 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<Passenger> passengers = repository.findAll(Sort.by(Sort.Direction.ASC, "seatNumber"));
|
||||
|
||||
assertThat(passengers, contains(fred, ricki, jill, siya, eve));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue