committed by
Oliver Gierke
parent
11a6e4b693
commit
5af654a12a
@@ -17,6 +17,8 @@ package example.springdata.jpa.simple;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Slice;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
@@ -81,4 +83,14 @@ public interface SimpleUserRepository extends CrudRepository<User, Long> {
|
||||
* @return
|
||||
*/
|
||||
Long removeByLastname(String lastname);
|
||||
|
||||
/**
|
||||
* Returns a {@link Slice} counting a maximum number of {@link Pageable#getPageSize()} users matching given criteria
|
||||
* starting at {@link Pageable#getOffset()} without prior count of the total number of elements available.
|
||||
*
|
||||
* @param lastname
|
||||
* @param page
|
||||
* @return
|
||||
*/
|
||||
Slice<User> findByLastnameOrderByUsernameAsc(String lastname, Pageable page);
|
||||
}
|
||||
|
||||
@@ -16,8 +16,10 @@
|
||||
package example.springdata.jpa.simple;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.collection.IsIterableContainingInOrder.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@@ -25,6 +27,8 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
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.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@@ -108,4 +112,28 @@ public class SimpleUserRepositoryTests {
|
||||
assertThat(repository.removeByLastname(user.getLastname()), is(2L));
|
||||
assertThat(repository.exists(user3.getId()), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void useSliceToLoadContent() {
|
||||
|
||||
repository.deleteAll();
|
||||
|
||||
// int repository with some values that can be ordered
|
||||
int totalNumberUsers = 11;
|
||||
List<User> source = new ArrayList<User>(totalNumberUsers);
|
||||
|
||||
for (int i = 1; i <= totalNumberUsers; i++) {
|
||||
|
||||
User user = new User();
|
||||
user.setLastname(this.user.getLastname());
|
||||
user.setUsername(user.getLastname() + "-" + String.format("%03d", i));
|
||||
source.add(user);
|
||||
}
|
||||
|
||||
repository.save(source);
|
||||
|
||||
Slice<User> users = repository.findByLastnameOrderByUsernameAsc(this.user.getLastname(), new PageRequest(1, 5));
|
||||
|
||||
assertThat(users, contains(source.subList(5, 10).toArray()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user