DATACASS-525 - Throw IncorrectResultSizeDataAccessException for repository query methods returning a single element.

We now throw IncorrectResultSizeDataAccessException if a single entity query yields more result rows. Previously we gracefully returned the first element of the query without hinting whether the result was unique or not.
This commit is contained in:
Mark Paluch
2018-02-14 14:20:10 +01:00
parent 8efa1bc7a1
commit 847deccccc
12 changed files with 148 additions and 30 deletions

View File

@@ -186,7 +186,9 @@ public interface PersonRepository extends CrudRepository<Person, String> {
Person findByShippingAddress(Address address); <5>
Stream<Person> findAllBy(); <6>
Person findFirstByShippingAddress(Address address); <6>
Stream<Person> findAllBy(); <7>
}
----
<1> The method shows a query for all people with the given `lastname`. The query will be derived from parsing
@@ -196,9 +198,11 @@ a query expression of `SELECT * from person WHERE lastname = 'lastname'`.
<3> Passing a `QueryOptions` object will apply the query options to the resulting query before it's execution.
<4> 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.
<5> Shows that you can query based on properties which are not a primitive type using registered `Converter`'s
in `CustomConversions`.
<6> Uses a Java 8 `Stream` which reads and converts individual elements while iterating the stream.
<5> Shows that you can query based on properties which are not a primitive type using registered ``Converter``'s
in `CustomConversions`. Throws `IncorrectResultSizeDataAccessException` if more than one match found.
<6> Uses the `First` keyword to restrict the query to the very first result. Unlike 5, this method does
not throw an exception if more than one match was found.
<7> Uses a Java 8 `Stream` which reads and converts individual elements while iterating the stream.
====
NOTE: Querying non-primary key properties requires secondary indexes.

View File

@@ -76,16 +76,19 @@ regards properties named "id" as the row id.
----
public interface ReactivePersonRepository extends ReactiveSortingRepository<Person, Long> {
@AllowFiltering
Flux<Person> findByFirstname(String firstname);
Flux<Person> findByFirstname(String firstname); <1>
@AllowFiltering
Flux<Person> findByFirstname(Publisher<String> firstname);
Flux<Person> findByFirstname(Publisher<String> firstname); <2>
@AllowFiltering
Mono<Person> findByFirstnameAndLastname(String firstname, String lastname);
Mono<Person> findByFirstnameAndLastname(String firstname, String lastname); <3>
Mono<Person> findFirstByFirstname(String firstname); <4>
}
----
<1> The method shows a query for all people with the given firstname. The query will be derived parsing the method name for constraints which can be concatenated with And and Or. Thus the method name will result in a query expression of `SELECT * FROM person WHERE firstname = :firstname`.
<2> The method shows a query for all people with the given firstname once the firstname is emitted via the given `Publisher`.
<3> Find a single entity for given criteria. Completes with `IncorrectResultSizeDataAccessException` on non unique results.
<4> Unlike 3, the first entity is always emitted even if the query yields more result rows.
====
For JavaConfig, use the `@EnableReactiveCassandraRepositories` annotation. The annotation carries the very same attributes
@@ -144,4 +147,3 @@ The following features are supported:
* <<projections>>
NOTE: Query methods must return a reactive type. Resolved types (`User` vs. `Mono<User>`) are not supported.