#15 - Added example for top/first keywords in repository methods.
This commit is contained in:
committed by
Oliver Gierke
parent
5af654a12a
commit
f4266ac211
@@ -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<User, Long> {
|
||||
* @return
|
||||
*/
|
||||
Slice<User> findByLastnameOrderByUsernameAsc(String lastname, Pageable page);
|
||||
|
||||
/**
|
||||
* Return the first 2 users ordered by their lastname asc.
|
||||
*
|
||||
* <pre>
|
||||
* Example for findFirstK / findTopK functionality.
|
||||
* </pre>
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
List<User> findFirst2ByOrderByLastnameAsc();
|
||||
|
||||
/**
|
||||
* Return the first 2 users ordered by the given {@code sort} definition.
|
||||
*
|
||||
* <pre>
|
||||
* 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.
|
||||
* </pre>
|
||||
*
|
||||
* @param sort
|
||||
* @return
|
||||
*/
|
||||
List<User> findTop2By(Sort sort);
|
||||
}
|
||||
|
||||
@@ -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<User> 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<User> resultAsc = repository.findTop2By(new Sort(ASC, "lastname"));
|
||||
|
||||
assertThat(resultAsc.size(), is(2));
|
||||
assertThat(resultAsc, hasItems(user0, user1));
|
||||
|
||||
List<User> resultDesc = repository.findTop2By(new Sort(DESC, "lastname"));
|
||||
|
||||
assertThat(resultDesc.size(), is(2));
|
||||
assertThat(resultDesc, hasItems(user1, user2));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user