Skip to content

Commit 4da92bd

Browse files
ada-wafflesmp911de
authored andcommitted
#407 - Add ReactiveSortingRepository support.
Implements ReactiveSortingRepository on SimpleR2dbcRepository. Also changed R2dbcRepository to extend ReactiveSortingRepository and updated comments where it felt reasonable. Added a single unit test for the new method, and changed the base interface of LegoSetRepository in AbstractR2dbcRepositoryIntegrationTests for integration testing purposes. Clarify documentation on reactive repository base interfaces Adds some language calling out ReactiveSortingRepository and fixes consistency between related examples. Original pull request: #408.
1 parent d515868 commit 4da92bd

File tree

6 files changed

+46
-11
lines changed

6 files changed

+46
-11
lines changed

src/main/asciidoc/reference/r2dbc-repositories.adoc

+4-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ The following example shows a repository interface for the preceding `Person` cl
3535
====
3636
[source]
3737
----
38-
public interface PersonRepository extends PagingAndSortingRepository<Person, String> {
38+
public interface PersonRepository extends ReactiveCrudRepository<Person, Long> {
3939
4040
// additional custom query methods go here
4141
}
@@ -62,7 +62,8 @@ class ApplicationConfig extends AbstractR2dbcConfiguration {
6262
----
6363
====
6464

65-
Because our domain repository extends `ReactiveCrudRepository`, it provides you with CRUD operations to access the entities.
65+
Because our domain repository extends `ReactiveCrudRepository`, it provides you with reactive CRUD operations to access the entities.
66+
On top of `ReactiveCrudRepository`, there is also `ReactiveSortingRepository`, which adds additional sorting functionality similar to that of `PagingAndSortingRepository`.
6667
Working with the repository instance is merely a matter of dependency injecting it into a client.
6768
Consequently, you can retrieve all `Person` objects with the following code:
6869

@@ -111,7 +112,7 @@ Defining such a query is a matter of declaring a method on the repository interf
111112
====
112113
[source,java]
113114
----
114-
interface ReactivePersonRepository extends ReactiveSortingRepository<Person, String> {
115+
interface ReactivePersonRepository extends ReactiveSortingRepository<Person, Long> {
115116
116117
Flux<Person> findByFirstname(String firstname); <1>
117118

src/main/java/org/springframework/data/r2dbc/repository/R2dbcRepository.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
package org.springframework.data.r2dbc.repository;
1717

1818
import org.springframework.data.repository.NoRepositoryBean;
19-
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
19+
import org.springframework.data.repository.reactive.ReactiveSortingRepository;
2020

2121
/**
2222
* R2DBC specific {@link org.springframework.data.repository.Repository} interface with reactive support.
2323
*
2424
* @author Mark Paluch
25+
* @author Stephen Cohen
2526
*/
2627
@NoRepositoryBean
27-
public interface R2dbcRepository<T, ID> extends ReactiveCrudRepository<T, ID> {}
28+
public interface R2dbcRepository<T, ID> extends ReactiveSortingRepository<T, ID> {}

src/main/java/org/springframework/data/r2dbc/repository/support/R2dbcRepositoryFactoryBean.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* @author Mark Paluch
3636
* @author Christoph Strobl
37-
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository
37+
* @see org.springframework.data.repository.reactive.ReactiveSortingRepository
3838
*/
3939
public class R2dbcRepositoryFactoryBean<T extends Repository<S, ID>, S, ID extends Serializable>
4040
extends RepositoryFactoryBeanSupport<T, S, ID> {

src/main/java/org/springframework/data/r2dbc/repository/support/SimpleR2dbcRepository.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.reactivestreams.Publisher;
2222

23+
import org.springframework.data.domain.Sort;
2324
import org.springframework.data.r2dbc.convert.R2dbcConverter;
2425
import org.springframework.data.r2dbc.core.DatabaseClient;
2526
import org.springframework.data.r2dbc.core.R2dbcEntityOperations;
@@ -29,20 +30,21 @@
2930
import org.springframework.data.relational.core.mapping.RelationalPersistentProperty;
3031
import org.springframework.data.relational.core.query.Query;
3132
import org.springframework.data.relational.repository.query.RelationalEntityInformation;
32-
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
33+
import org.springframework.data.repository.reactive.ReactiveSortingRepository;
3334
import org.springframework.data.util.Lazy;
3435
import org.springframework.transaction.annotation.Transactional;
3536
import org.springframework.util.Assert;
3637

3738
/**
38-
* Simple {@link ReactiveCrudRepository} implementation using R2DBC through {@link DatabaseClient}.
39+
* Simple {@link ReactiveSortingRepository} implementation using R2DBC through {@link DatabaseClient}.
3940
*
4041
* @author Mark Paluch
4142
* @author Jens Schauder
4243
* @author Mingyuan Wu
44+
* @author Stephen Cohen
4345
*/
4446
@Transactional(readOnly = true)
45-
public class SimpleR2dbcRepository<T, ID> implements ReactiveCrudRepository<T, ID> {
47+
public class SimpleR2dbcRepository<T, ID> implements ReactiveSortingRepository<T, ID> {
4648

4749
private final RelationalEntityInformation<T, ID> entity;
4850
private final R2dbcEntityOperations entityOperations;
@@ -172,6 +174,14 @@ public Flux<T> findAll() {
172174
return this.entityOperations.select(Query.empty(), this.entity.getJavaType());
173175
}
174176

177+
/* (non-Javadoc)
178+
* @see org.springframework.data.repository.reactive.ReactiveSortingRepository#findAll(org.springframework.data.domain.Sort)
179+
*/
180+
@Override
181+
public Flux<T> findAll(Sort sort) {
182+
return this.entityOperations.select(Query.empty().sort(sort), this.entity.getJavaType());
183+
}
184+
175185
/* (non-Javadoc)
176186
* @see org.springframework.data.repository.reactive.ReactiveCrudRepository#findAllById(java.lang.Iterable)
177187
*/

src/test/java/org/springframework/data/r2dbc/repository/AbstractR2dbcRepositoryIntegrationTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,15 @@
4848
import org.springframework.data.r2dbc.testing.R2dbcIntegrationTestSupport;
4949
import org.springframework.data.relational.core.mapping.Table;
5050
import org.springframework.data.repository.NoRepositoryBean;
51-
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
51+
import org.springframework.data.repository.reactive.ReactiveSortingRepository;
5252
import org.springframework.jdbc.core.JdbcTemplate;
5353
import org.springframework.transaction.reactive.TransactionalOperator;
5454

5555
/**
5656
* Abstract base class for integration tests for {@link LegoSetRepository} using {@link R2dbcRepositoryFactory}.
5757
*
5858
* @author Mark Paluch
59+
* @author Stephen Cohen
5960
*/
6061
public abstract class AbstractR2dbcRepositoryIntegrationTests extends R2dbcIntegrationTestSupport {
6162

@@ -303,7 +304,7 @@ private Condition<? super Object> numberOf(int expected) {
303304
}
304305

305306
@NoRepositoryBean
306-
interface LegoSetRepository extends ReactiveCrudRepository<LegoSet, Integer> {
307+
interface LegoSetRepository extends ReactiveSortingRepository<LegoSet, Integer> {
307308

308309
Flux<LegoSet> findByNameContains(String name);
309310

src/test/java/org/springframework/data/r2dbc/repository/support/AbstractSimpleR2dbcRepositoryIntegrationTests.java

+22
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.data.annotation.Id;
4040
import org.springframework.data.annotation.Version;
4141
import org.springframework.data.domain.Persistable;
42+
import org.springframework.data.domain.Sort;
4243
import org.springframework.data.r2dbc.convert.MappingR2dbcConverter;
4344
import org.springframework.data.r2dbc.core.DatabaseClient;
4445
import org.springframework.data.r2dbc.core.ReactiveDataAccessStrategy;
@@ -55,6 +56,7 @@
5556
*
5657
* @author Mark Paluch
5758
* @author Bogdan Ilchyshyn
59+
* @author Stephen Cohen
5860
*/
5961
public abstract class AbstractSimpleR2dbcRepositoryIntegrationTests extends R2dbcIntegrationTestSupport {
6062

@@ -313,6 +315,26 @@ public void shouldFindByAll() {
313315
}).verifyComplete();
314316
}
315317

318+
@Test // gh-407
319+
public void shouldFindAllWithSort() {
320+
321+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('FORSCHUNGSSCHIFF', 13)");
322+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('SCHAUFELRADBAGGER', 12)");
323+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('VOLTRON', 15)");
324+
jdbc.execute("INSERT INTO legoset (name, manual) VALUES('RALLYEAUTO', 14)");
325+
326+
repository.findAll(Sort.by("manual").ascending()) //
327+
.map(LegoSet::getName) //
328+
.collectList() //
329+
.as(StepVerifier::create) //
330+
.assertNext(actual -> assertThat(actual).containsExactly(
331+
"SCHAUFELRADBAGGER",
332+
"FORSCHUNGSSCHIFF",
333+
"RALLYEAUTO",
334+
"VOLTRON"
335+
)).verifyComplete();
336+
}
337+
316338
@Test
317339
public void shouldFindAllByIdUsingIterable() {
318340

0 commit comments

Comments
 (0)