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
@@ -18,6 +18,7 @@ package example.springdata.cassandra.basic;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.cassandra.repository.Query;
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
/**
|
||||
@@ -55,4 +56,13 @@ public interface BasicUserRepository extends CrudRepository<User, Long> {
|
||||
* @return
|
||||
*/
|
||||
List<User> findUsersByLastnameStartsWith(String lastnamePrefix);
|
||||
|
||||
/**
|
||||
* Same as {@link #findUsersByLastnameStartsWith(String)} but reducing the result size to a given {@link Limit}.
|
||||
*
|
||||
* @param lastnamePrefix
|
||||
* @param maxResults the maximum number of results returned.
|
||||
* @return
|
||||
*/
|
||||
List<User> findUsersByLastnameStartsWith(String lastnamePrefix, Limit maxResults);
|
||||
}
|
||||
|
||||
@@ -43,4 +43,10 @@ public class User {
|
||||
public User(Long id) {
|
||||
this.setId(id);
|
||||
}
|
||||
|
||||
public User(Long id, String firstname, String lastname) {
|
||||
this.id = id;
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ package example.springdata.cassandra.basic;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.assertj.core.api.Assumptions.*;
|
||||
|
||||
import java.util.stream.LongStream;
|
||||
|
||||
import example.springdata.cassandra.util.CassandraKeyspace;
|
||||
import example.springdata.cassandra.util.CassandraVersion;
|
||||
|
||||
@@ -26,6 +28,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.util.Version;
|
||||
|
||||
import com.datastax.oss.driver.api.core.CqlSession;
|
||||
@@ -120,4 +123,24 @@ class BasicUserRepositoryTests {
|
||||
|
||||
assertThat(repository.findUsersByLastnameStartsWith("last")).contains(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Spring Data Cassandra supports {@code Limit} to reduce the number of returned results.
|
||||
*/
|
||||
@Test
|
||||
void limitResultSize() throws InterruptedException {
|
||||
|
||||
assumeThat(CassandraVersion.getReleaseVersion(session).isGreaterThanOrEqualTo(CASSANDRA_3_4)).isTrue();
|
||||
|
||||
session.execute("CREATE CUSTOM INDEX ON users (lname) USING 'org.apache.cassandra.index.sasi.SASIIndex';");
|
||||
/*
|
||||
Cassandra secondary indexes are created in the background without the possibility to check
|
||||
whether they are available or not. So we are forced to just wait. *sigh*
|
||||
*/
|
||||
Thread.sleep(1000);
|
||||
|
||||
LongStream.range(0, 10).forEach(id -> repository.save(new User(id, user.getFirstname(), user.getLastname())));
|
||||
|
||||
assertThat(repository.findUsersByLastnameStartsWith("last", Limit.of(5))).hasSize(5);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package example.springdata.cassandra.people;
|
||||
|
||||
import org.springframework.data.domain.Limit;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -36,6 +37,15 @@ public interface ReactivePersonRepository extends ReactiveCrudRepository<Person,
|
||||
*/
|
||||
Flux<Person> findByLastname(String lastname);
|
||||
|
||||
/**
|
||||
* Derived query selecting by {@code lastname} reducing the result size to a given {@link Limit}.
|
||||
*
|
||||
* @param lastname
|
||||
* @param maxResults the maximum number of results returned.
|
||||
* @return
|
||||
*/
|
||||
Flux<Person> findByLastname(String lastname, Limit maxResults);
|
||||
|
||||
/**
|
||||
* String query selecting one entity.
|
||||
*
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package example.springdata.cassandra.people;
|
||||
|
||||
import example.springdata.cassandra.util.CassandraKeyspace;
|
||||
import org.springframework.data.domain.Limit;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
@@ -89,6 +90,14 @@ class ReactivePersonRepositoryIntegrationTest {
|
||||
StepVerifier.create(repository.findByLastname("White")).expectNextCount(2).verifyComplete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data limiting result size.
|
||||
*/
|
||||
@Test
|
||||
void limitResultSize() {
|
||||
StepVerifier.create(repository.findByLastname("White", Limit.of(1))).expectNextCount(1).verifyComplete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data using a string query.
|
||||
*/
|
||||
|
||||
@@ -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() {
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package example.springdata.mongodb.customer;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.GeoResults;
|
||||
@@ -41,6 +42,15 @@ public interface CustomerRepository extends CrudRepository<Customer, String> {
|
||||
*/
|
||||
List<Customer> findByLastname(String lastname, Sort sort);
|
||||
|
||||
/**
|
||||
* Derived query reducing result size to a given {@link Limit}.
|
||||
*
|
||||
* @param lastname
|
||||
* @param maxResults the maximum number of results returned.
|
||||
* @return
|
||||
*/
|
||||
List<Customer> findByLastname(String lastname, Limit maxResults);
|
||||
|
||||
/**
|
||||
* Showcase for a repository query using geospatial functionality.
|
||||
*
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
|
||||
import org.springframework.data.domain.Limit;
|
||||
import org.springframework.data.geo.Distance;
|
||||
import org.springframework.data.geo.Metrics;
|
||||
import org.springframework.data.geo.Point;
|
||||
@@ -96,6 +97,17 @@ class CustomerRepositoryIntegrationTest {
|
||||
assertThat(result.get(1)).isEqualTo(oliver);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case to show how to reduce result size with dynamic {@link Limit}.
|
||||
*/
|
||||
@Test
|
||||
void limitResultSize() {
|
||||
|
||||
var result = repository.findByLastname("Matthews", Limit.of(1));
|
||||
|
||||
assertThat(result).hasSize(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test case to show the usage of Java {@link Stream}.
|
||||
*/
|
||||
|
||||
@@ -15,6 +15,9 @@
|
||||
*/
|
||||
package example.springdata.mongodb.people;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.domain.Limit;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
@@ -37,6 +40,17 @@ public interface ReactivePersonRepository extends ReactiveCrudRepository<Person,
|
||||
*/
|
||||
Flux<Person> 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
|
||||
*/
|
||||
Flux<Person> findByLastname(String lastname, Limit maxResults);
|
||||
|
||||
/**
|
||||
* String query selecting one entity.
|
||||
*
|
||||
|
||||
@@ -18,6 +18,7 @@ package example.springdata.mongodb.people;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import example.springdata.mongodb.util.MongoContainers;
|
||||
import org.springframework.data.domain.Limit;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
@@ -157,6 +158,14 @@ class ReactivePersonRepositoryIntegrationTest {
|
||||
repository.findByLastname("White").as(StepVerifier::create).expectNextCount(2).verifyComplete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Limit result size.
|
||||
*/
|
||||
@Test
|
||||
void shouldLimitResultSize() {
|
||||
repository.findByLastname("White", Limit.of(1)).as(StepVerifier::create).expectNextCount(1).verifyComplete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch data using a string query.
|
||||
*/
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package example.springdata.r2dbc.basics;
|
||||
|
||||
import org.springframework.data.domain.Limit;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.data.r2dbc.repository.Query;
|
||||
@@ -28,4 +29,6 @@ interface CustomerRepository extends ReactiveCrudRepository<Customer, Long> {
|
||||
|
||||
@Query("select id, firstname, lastname from customer c where c.lastname = :lastname")
|
||||
Flux<Customer> findByLastname(String lastname);
|
||||
|
||||
Flux<Customer> findByLastname(String lastname, Limit limit);
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package example.springdata.r2dbc.basics;
|
||||
|
||||
import org.springframework.data.domain.Limit;
|
||||
import reactor.core.publisher.Hooks;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
@@ -72,6 +73,25 @@ class TransactionalServiceIntegrationTests {
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test
|
||||
void limitResultSize() {
|
||||
|
||||
service.save(new Customer(null, "Carter", "Matthews")) //
|
||||
.as(StepVerifier::create) //
|
||||
.expectNextMatches(Customer::hasId) //
|
||||
.verifyComplete();
|
||||
|
||||
service.save(new Customer(null, "Evad", "Matthews")) //
|
||||
.as(StepVerifier::create) //
|
||||
.expectNextMatches(Customer::hasId) //
|
||||
.verifyComplete();
|
||||
|
||||
repository.findByLastname("Matthews", Limit.of(1)) //
|
||||
.as(StepVerifier::create) //
|
||||
.expectNextCount(1)
|
||||
.verifyComplete();
|
||||
}
|
||||
|
||||
@Test // #500
|
||||
void insertsDataTransactionally() {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user