Skip to content

Commit 7d69b84

Browse files
committed
DATAMONGO-1030 - Projections now work on single-entity query method executions.
We now correctly forward the domain type collection to the query executing a query for a projection type.
1 parent 4eaef30 commit 7d69b84

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ Object execute(Query query) {
317317

318318
MongoEntityMetadata<?> metadata = method.getEntityInformation();
319319
return countProjection ? operations.count(query, metadata.getJavaType()) : operations.findOne(query,
320-
metadata.getJavaType());
320+
metadata.getJavaType(), metadata.getCollectionName());
321321
}
322322
}
323323

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,4 +1011,18 @@ public void shouldExecuteFindOnDbRefCorrectly() {
10111011
public void shouldFindPersonsWhenUsingQueryDslPerdicatedOnIdProperty() {
10121012
assertThat(repository.findAll(person.id.in(Arrays.asList(dave.id, carter.id))), containsInAnyOrder(dave, carter));
10131013
}
1014+
1015+
/**
1016+
* @see DATAMONGO-1030
1017+
*/
1018+
@Test
1019+
public void executesSingleEntityQueryWithProjectionCorrectly() {
1020+
1021+
PersonSummary result = repository.findSummaryByLastname("Beauford");
1022+
1023+
assertThat(result, is(notNullValue()));
1024+
assertThat(result.firstname, is("Carter"));
1025+
assertThat(result.lastname, is("Beauford"));
1026+
1027+
}
10141028
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,4 +313,8 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
313313
*/
314314
Page<Person> findTop3ByLastnameStartingWith(String lastname, Pageable pageRequest);
315315

316+
/**
317+
* @see DATAMONGO-1030
318+
*/
319+
PersonSummary findSummaryByLastname(String lastname);
316320
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2014 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;
17+
18+
/**
19+
* @author Oliver Gierke
20+
*/
21+
public class PersonSummary {
22+
23+
String firstname;
24+
String lastname;
25+
}

0 commit comments

Comments
 (0)