Skip to content

Commit b7f4fde

Browse files
committed
DATAMONGO-1702 - Adopt to composable repositories.
1 parent 61f323c commit b7f4fde

File tree

7 files changed

+153
-44
lines changed

7 files changed

+153
-44
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/MongoRepositoryFactory.java

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@
3636
import org.springframework.data.repository.core.NamedQueries;
3737
import org.springframework.data.repository.core.RepositoryInformation;
3838
import org.springframework.data.repository.core.RepositoryMetadata;
39+
import org.springframework.data.repository.core.support.RepositoryComposition;
3940
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
41+
import org.springframework.data.repository.core.support.RepositoryFragment;
4042
import org.springframework.data.repository.query.EvaluationContextProvider;
4143
import org.springframework.data.repository.query.QueryLookupStrategy;
4244
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
4345
import org.springframework.data.repository.query.RepositoryQuery;
44-
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
45-
import org.springframework.data.repository.reactive.RxJava1CrudRepository;
4646
import org.springframework.data.repository.util.QueryExecutionConverters;
4747
import org.springframework.expression.spel.standard.SpelExpressionParser;
4848
import org.springframework.util.Assert;
@@ -88,25 +88,38 @@ public MongoRepositoryFactory(MongoOperations mongoOperations) {
8888
*/
8989
@Override
9090
protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {
91+
return SimpleMongoRepository.class;
92+
}
93+
94+
/*
95+
* (non-Javadoc)
96+
* @see org.springframework.data.repository.core.support.RepositoryFactorySupport#getRepositoryCompositition(org.springframework.data.repository.core.RepositoryMetadata)
97+
*/
98+
@Override
99+
protected RepositoryComposition getRepositoryCompositition(RepositoryMetadata metadata) {
100+
101+
RepositoryComposition composition = super.getRepositoryCompositition(metadata);
91102

92-
boolean isReactiveRepository = (PROJECT_REACTOR_PRESENT
93-
&& ReactiveCrudRepository.class.isAssignableFrom(metadata.getRepositoryInterface()))
94-
|| (RXJAVA_OBSERVABLE_PRESENT
95-
&& RxJava1CrudRepository.class.isAssignableFrom(metadata.getRepositoryInterface()));
103+
RepositoryInformation information = getRepositoryInformation(metadata, composition);
96104

97105
boolean isQueryDslRepository = QUERY_DSL_PRESENT
98106
&& QuerydslPredicateExecutor.class.isAssignableFrom(metadata.getRepositoryInterface());
99107

100-
if (isReactiveRepository) {
108+
if (isQueryDslRepository) {
101109

102-
if (isQueryDslRepository) {
110+
if (metadata.isReactiveRepository()) {
103111
throw new InvalidDataAccessApiUsageException(
104112
"Cannot combine Querydsl and reactive repository in one interface");
105113
}
106-
return SimpleReactiveMongoRepository.class;
114+
115+
MongoEntityInformation<?, Serializable> entityInformation = getEntityInformation(information.getDomainType(),
116+
information);
117+
118+
composition = composition.append(RepositoryFragment
119+
.implemented(getTargetRepositoryViaReflection(QuerydslMongoRepository.class, entityInformation, operations)));
107120
}
108121

109-
return isQueryDslRepository ? QuerydslMongoRepository.class : SimpleMongoRepository.class;
122+
return composition;
110123
}
111124

112125
/*
@@ -149,7 +162,7 @@ private <T, ID> MongoEntityInformation<T, ID> getEntityInformation(Class<T> doma
149162

150163
/**
151164
* {@link QueryLookupStrategy} to create {@link PartTreeMongoQuery} instances.
152-
*
165+
*
153166
* @author Oliver Gierke
154167
* @author Thomas Darimont
155168
*/

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/QuerydslMongoRepository.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@
5252
* @author Mark Paluch
5353
* @author Christoph Strobl
5454
*/
55-
public class QuerydslMongoRepository<T, ID extends Serializable> extends SimpleMongoRepository<T, ID>
56-
implements QuerydslPredicateExecutor<T> {
55+
public class QuerydslMongoRepository<T, ID extends Serializable> implements QuerydslPredicateExecutor<T> {
5756

5857
private final PathBuilder<T> builder;
5958
private final EntityInformation<T, ID> entityInformation;
@@ -81,8 +80,6 @@ public QuerydslMongoRepository(MongoEntityInformation<T, ID> entityInformation,
8180
public QuerydslMongoRepository(MongoEntityInformation<T, ID> entityInformation, MongoOperations mongoOperations,
8281
EntityPathResolver resolver) {
8382

84-
super(entityInformation, mongoOperations);
85-
8683
Assert.notNull(resolver, "EntityPathResolver must not be null!");
8784

8885
EntityPath<T> path = resolver.createPath(entityInformation.getJavaType());
@@ -174,33 +171,6 @@ public Page<T> findAll(Predicate predicate, Pageable pageable) {
174171
() -> createQueryFor(predicate).fetchCount());
175172
}
176173

177-
/*
178-
* (non-Javadoc)
179-
* @see org.springframework.data.mongodb.repository.support.SimpleMongoRepository#findAll(org.springframework.data.domain.Pageable)
180-
*/
181-
@Override
182-
public Page<T> findAll(Pageable pageable) {
183-
184-
Assert.notNull(pageable, "Pageable must not be null!");
185-
186-
AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> query = createQuery();
187-
188-
return PageableExecutionUtils.getPage(applyPagination(query, pageable).fetchResults().getResults(), pageable,
189-
() -> createQuery().fetchCount());
190-
}
191-
192-
/*
193-
* (non-Javadoc)
194-
* @see org.springframework.data.mongodb.repository.support.SimpleMongoRepository#findAll(org.springframework.data.domain.Sort)
195-
*/
196-
@Override
197-
public List<T> findAll(Sort sort) {
198-
199-
Assert.notNull(sort, "Sort must not be null!");
200-
201-
return applySorting(createQuery(), sort).fetchResults().getResults();
202-
}
203-
204174
/*
205175
* (non-Javadoc)
206176
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#count(com.mysema.query.types.Predicate)
@@ -237,7 +207,7 @@ private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> createQueryFor(Predic
237207

238208
/**
239209
* Creates a {@link MongodbQuery}.
240-
*
210+
*
241211
* @return
242212
*/
243213
private AbstractMongodbQuery<T, SpringDataMongodbQuery<T>> createQuery() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.repository.custom;
17+
18+
import org.springframework.data.mongodb.core.User;
19+
import org.springframework.data.repository.Repository;
20+
21+
/**
22+
* @author Mark Paluch
23+
*/
24+
public interface ComposedRepository extends Repository<User, String>, RepositoryMixin {
25+
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.repository.custom;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.context.annotation.ImportResource;
25+
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
26+
import org.springframework.test.context.ContextConfiguration;
27+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
28+
29+
/**
30+
* Integration tests for composed Repository implementations.
31+
*
32+
* @author Mark Paluch
33+
*/
34+
@RunWith(SpringJUnit4ClassRunner.class)
35+
@ContextConfiguration
36+
public class ComposedRepositoryImplementationTests {
37+
38+
@Configuration
39+
@EnableMongoRepositories
40+
@ImportResource("classpath:infrastructure.xml")
41+
static class Config {}
42+
43+
@Autowired ComposedRepository composedRepository;
44+
45+
@Test // DATAMONGO-1702
46+
public void shouldExecuteMethodOnCustomRepositoryImplementation() {
47+
assertThat(composedRepository.getFoo()).isEqualTo("foo");
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.repository.custom;
17+
18+
/**
19+
* @author Mark Paluch
20+
*/
21+
public interface RepositoryMixin {
22+
23+
String getFoo();
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.repository.custom;
17+
18+
/**
19+
* @author Mark Paluch
20+
*/
21+
public class RepositoryMixinImpl implements RepositoryMixin {
22+
23+
@Override
24+
public String getFoo() {
25+
return "foo";
26+
}
27+
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/support/QueryDslMongoRepositoryIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void setup() {
6767

6868
person = new QPerson("person");
6969

70-
repository.saveAll(Arrays.asList(oliver, dave, carter));
70+
operations.insertAll(Arrays.asList(oliver, dave, carter));
7171
}
7272

7373
@Test // DATAMONGO-1146

0 commit comments

Comments
 (0)