From f4266ac211dda6f45e2bf06862d44258fa9f8a88 Mon Sep 17 00:00:00 2001 From: Thomas Darimont Date: Thu, 4 Sep 2014 13:15:33 +0200 Subject: [PATCH] #15 - Added example for top/first keywords in repository methods. --- .../jpa/simple/SimpleUserRepository.java | 25 ++++++++++ .../jpa/simple/SimpleUserRepositoryTests.java | 49 +++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/jpa/example/src/main/java/example/springdata/jpa/simple/SimpleUserRepository.java b/jpa/example/src/main/java/example/springdata/jpa/simple/SimpleUserRepository.java index 63f39cbf..858d72b0 100644 --- a/jpa/example/src/main/java/example/springdata/jpa/simple/SimpleUserRepository.java +++ b/jpa/example/src/main/java/example/springdata/jpa/simple/SimpleUserRepository.java @@ -19,6 +19,7 @@ import java.util.List; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; +import org.springframework.data.domain.Sort; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; @@ -93,4 +94,28 @@ public interface SimpleUserRepository extends CrudRepository { * @return */ Slice findByLastnameOrderByUsernameAsc(String lastname, Pageable page); + + /** + * Return the first 2 users ordered by their lastname asc. + * + *
+	 * Example for findFirstK / findTopK functionality.
+	 * 
+ * + * @return + */ + List findFirst2ByOrderByLastnameAsc(); + + /** + * Return the first 2 users ordered by the given {@code sort} definition. + * + *
+	 * This variant is very flexible because one can ask for the first K results when a ASC ordering
+	 * is used as well as for the last K results when a DESC ordering is used.
+	 * 
+ * + * @param sort + * @return + */ + List findTop2By(Sort sort); } diff --git a/jpa/example/src/test/java/example/springdata/jpa/simple/SimpleUserRepositoryTests.java b/jpa/example/src/test/java/example/springdata/jpa/simple/SimpleUserRepositoryTests.java index 20ba8fdb..14310902 100644 --- a/jpa/example/src/test/java/example/springdata/jpa/simple/SimpleUserRepositoryTests.java +++ b/jpa/example/src/test/java/example/springdata/jpa/simple/SimpleUserRepositoryTests.java @@ -18,6 +18,7 @@ package example.springdata.jpa.simple; import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.collection.IsIterableContainingInOrder.*; import static org.junit.Assert.*; +import static org.springframework.data.domain.Sort.Direction.*; import java.util.ArrayList; import java.util.Arrays; @@ -29,6 +30,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Slice; +import org.springframework.data.domain.Sort; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional; @@ -136,4 +138,51 @@ public class SimpleUserRepositoryTests { assertThat(users, contains(source.subList(5, 10).toArray())); } + + @Test + public void findFirst2ByOrderByLastnameAsc() { + + User user0 = new User(); + user0.setLastname("lastname-0"); + + User user1 = new User(); + user1.setLastname("lastname-1"); + + User user2 = new User(); + user2.setLastname("lastname-2"); + + // we deliberatly save the items in reverse + repository.save(Arrays.asList(user2, user1, user0)); + + List result = repository.findFirst2ByOrderByLastnameAsc(); + + assertThat(result.size(), is(2)); + assertThat(result, hasItems(user0, user1)); + } + + @Test + public void findTop2ByWithSort() { + + User user0 = new User(); + user0.setLastname("lastname-0"); + + User user1 = new User(); + user1.setLastname("lastname-1"); + + User user2 = new User(); + user2.setLastname("lastname-2"); + + // we deliberately save the items in reverse + repository.save(Arrays.asList(user2, user1, user0)); + + List resultAsc = repository.findTop2By(new Sort(ASC, "lastname")); + + assertThat(resultAsc.size(), is(2)); + assertThat(resultAsc, hasItems(user0, user1)); + + List resultDesc = repository.findTop2By(new Sort(DESC, "lastname")); + + assertThat(resultDesc.size(), is(2)); + assertThat(resultDesc, hasItems(user1, user2)); + } }