Show how to use Limit with repository queries.
Add methods taking a Limit parameter to various samples. Original pull request: #672 Closes #671
This commit is contained in:
committed by
Mark Paluch
parent
e90be0c65b
commit
f98c7b9c93
@@ -20,6 +20,7 @@ import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Slice;
|
||||
import org.springframework.data.domain.Sort;
|
||||
@@ -63,6 +64,17 @@ public interface SimpleUserRepository extends ListCrudRepository<User, Long> {
|
||||
*/
|
||||
List<User> findByLastname(String lastname);
|
||||
|
||||
/**
|
||||
* Find at most the number of users defined via maxResults with the given lastname.
|
||||
* This method will be translated into a query by constructing it directly from the method name as there is no other
|
||||
* query declared.
|
||||
*
|
||||
* @param lastname
|
||||
* @param maxResults the maximum number of results returned.
|
||||
* @return
|
||||
*/
|
||||
List<User> findByLastname(String lastname, Limit maxResults);
|
||||
|
||||
/**
|
||||
* Returns all users with the given firstname. This method will be translated into a query using the one declared in
|
||||
* the {@link Query} annotation declared one.
|
||||
@@ -73,6 +85,17 @@ public interface SimpleUserRepository extends ListCrudRepository<User, Long> {
|
||||
@Query("select u from User u where u.firstname = :firstname")
|
||||
List<User> findByFirstname(String firstname);
|
||||
|
||||
/**
|
||||
* Returns at most the number of users defined via {@link Limit} with the given firstname. This method will be
|
||||
* translated into a query using the one declared in the {@link Query} annotation declared one.
|
||||
*
|
||||
* @param firstname
|
||||
* @param maxResults the maximum number of results returned.
|
||||
* @return
|
||||
*/
|
||||
@Query("select u from User u where u.firstname = :firstname")
|
||||
List<User> findByFirstname(String firstname, Limit maxResults);
|
||||
|
||||
/**
|
||||
* Returns all users with the given name as first- or lastname. This makes the query to method relation much more
|
||||
* refactoring-safe as the order of the method parameters is completely irrelevant.
|
||||
|
||||
@@ -27,6 +27,7 @@ import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
@@ -36,6 +37,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
@@ -83,6 +85,22 @@ class SimpleUserRepositoryTests {
|
||||
assertThat(repository.findByLastname("lastname")).contains(user);
|
||||
}
|
||||
|
||||
@Test
|
||||
void findLimitedNumberOfUsersViaDerivedQuery() {
|
||||
|
||||
IntStream.range(0, 10).forEach($ -> repository.save(new User(user.getFirstname(), user.getLastname())));
|
||||
|
||||
assertThat(repository.findByLastname("lastname", Limit.of(5))).hasSize(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
void findLimitedNumberOfUsersViaAnnotatedQuery() {
|
||||
|
||||
IntStream.range(0, 10).forEach($ -> repository.save(new User(user.getFirstname(), user.getLastname())));
|
||||
|
||||
assertThat(repository.findByFirstname(user.getFirstname(), Limit.of(5))).hasSize(5);
|
||||
}
|
||||
|
||||
@Test
|
||||
void findByFirstnameOrLastname() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user