DATACASS-146 - Enhance Repository methods to accept QueryOptions as arguments.
We now support Repository query methods with query options. Query options can be passed either as an additional parameter to a Repository query method or applied with annotation.
Annotation-based query options are supported via @Consistency. A query options parameter has precedence over the annotation if a method declares both, an annotation-based consistency level and accepts a query options parameter.
interface SampleRepository extends Repository<Person, String> {
@Query("SELECT * FROM person WHERE lastname = ?0;")
@Consistency(ConsistencyLevel.LOCAL_ONE)
Person findByLastname(String lastname);
@Consistency(ConsistencyLevel.LOCAL_ONE)
Person findByAge(int age);
Person findByAge(int age, QueryOptions options);
}
SampleRepository repository = …;
repository.findByAge(42, QueryOptions.builder().fetchSize(44).build());
This commit is contained in:
@@ -147,23 +147,26 @@ the Apache Cassandra database. Defining such a query is just a matter of declari
|
||||
----
|
||||
public interface PersonRepository extends CrudRepository<Person, String> {
|
||||
|
||||
List<Person> findByLastname(String lastname); <1>
|
||||
List<Person> findByLastname(String lastname); <1>
|
||||
|
||||
List<Person> findByFirstname(String firstname, Sort sort); <2>
|
||||
List<Person> findByFirstname(String firstname, Sort sort); <2>
|
||||
|
||||
Person findByShippingAddress(Address address); <3>
|
||||
List<Person> findByFirstname(String firstname, QueryOptions opts); <3>
|
||||
|
||||
Stream<Person> findAllBy(); <4>
|
||||
Person findByShippingAddress(Address address); <4>
|
||||
|
||||
Stream<Person> findAllBy(); <5>
|
||||
}
|
||||
----
|
||||
<1> The method shows a query for all people with the given `lastname`. The query will be derived from parsing
|
||||
the method name for constraints which can be concatenated with `And`. Thus the method name will result in
|
||||
a query expression of `SELECT * from person WHERE lastname = 'lastname'`.
|
||||
<2> Applies dynamic sorting to a query. Just add a `Sort` parameter to your method signature and Spring Data
|
||||
<2> Applies dynamic sorting to a query. Just add a `Sort` parameter to your method signature and Spring Data.
|
||||
will automatically apply ordering to the query accordingly.
|
||||
<3> Shows that you can query based on properties which are not a primitive type using registered `Converter`'s
|
||||
<3> Passing a `QueryOptions` object will apply the query options to the resulting query before it's execution.
|
||||
<4> Shows that you can query based on properties which are not a primitive type using registered `Converter`'s.
|
||||
in `CustomConversions`.
|
||||
<4> Uses a Java 8 `Stream` which reads and converts individual elements while iterating the stream.
|
||||
<5> Uses a Java 8 `Stream` which reads and converts individual elements while iterating the stream.
|
||||
====
|
||||
|
||||
NOTE: Querying non-primary key properties requires secondary indexes.
|
||||
@@ -231,6 +234,31 @@ NOTE: Querying non-primary key properties requires secondary indexes.
|
||||
|
||||
include::../{spring-data-commons-docs}/repository-projections.adoc[leveloffset=+2]
|
||||
|
||||
=== Query options
|
||||
|
||||
You can specify query options for query methods by passing a `QueryOptions` object
|
||||
to apply options to the query before the actual query execution.
|
||||
`QueryOptions` is treated as non-query parameter and isn't considered as query parameter value.
|
||||
|
||||
For static declaration of a consistency level, use the `@Consistency` annotation on query methods.
|
||||
The declared consistency level is applied to the query each time it is executed.
|
||||
|
||||
Query options are applicable to derived and string `@Query` repository methods.
|
||||
|
||||
----
|
||||
public interface PersonRepository extends CrudRepository<Person, String> {
|
||||
|
||||
@Consistency(ConsistencyLevel.LOCAL_ONE)
|
||||
List<Person> findByLastname(String lastname);
|
||||
|
||||
List<Person> findByFirstname(String firstname, QueryOptions options);
|
||||
}
|
||||
----
|
||||
|
||||
NOTE: You can control fetch size, consistency level and retry policy defaults by configuring these parameters
|
||||
on the CQL API instances `CqlTemplate`, `AsyncCqlTemplate`, and `ReactiveCqlTemplate`. Defaults apply if the particular
|
||||
query option is not set.
|
||||
|
||||
[[cassandra.repositories.misc]]
|
||||
== Miscellaneous
|
||||
|
||||
|
||||
@@ -664,6 +664,10 @@ All CQL issued by this class is logged at the `DEBUG` level under the category c
|
||||
name of the template instance (typically `CqlTemplate`, but it may be different if you are using a custom subclass
|
||||
of the `CqlTemplate` class).
|
||||
|
||||
You can control fetch size, consistency level and retry policy defaults by configuring these parameters
|
||||
on the CQL API instances `CqlTemplate`, `AsyncCqlTemplate`, and `ReactiveCqlTemplate`. Defaults apply if the particular
|
||||
query option is not set.
|
||||
|
||||
NOTE: `CqlTemplate` comes in different execution model flavors. The basic `CqlTemplate` uses a blocking execution model.
|
||||
You can use `AsyncCqlTemplate` for asynchronous execution and synchronization with ``ListenableFuture``s or
|
||||
<<cassandra.reactive.cql-template,`ReactiveCqlTemplate`>> for reactive execution.
|
||||
|
||||
Reference in New Issue
Block a user