Skip to content

Commit 440d16e

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 11c2e90 commit 440d16e

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-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
@@ -284,7 +284,7 @@ Object execute(Query query) {
284284

285285
MongoEntityMetadata<?> metadata = method.getEntityInformation();
286286
return countProjection ? operations.count(query, metadata.getJavaType()) : operations.findOne(query,
287-
metadata.getJavaType());
287+
metadata.getJavaType(), metadata.getCollectionName());
288288
}
289289
}
290290

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
@@ -949,4 +949,18 @@ public void shouldExecuteFindOnDbRefCorrectly() {
949949
public void shouldFindPersonsWhenUsingQueryDslPerdicatedOnIdProperty() {
950950
assertThat(repository.findAll(person.id.in(Arrays.asList(dave.id, carter.id))), containsInAnyOrder(dave, carter));
951951
}
952+
953+
/**
954+
* @see DATAMONGO-1030
955+
*/
956+
@Test
957+
public void executesSingleEntityQueryWithProjectionCorrectly() {
958+
959+
PersonSummary result = repository.findSummaryByLastname("Beauford");
960+
961+
assertThat(result, is(notNullValue()));
962+
assertThat(result.firstname, is("Carter"));
963+
assertThat(result.lastname, is("Beauford"));
964+
965+
}
952966
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,9 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
302302
*/
303303
@Query("{lastname:?0, address.street:{$in:?1}}")
304304
Page<Person> findByCustomQueryLastnameAndAddressStreetInList(String lastname, List<String> streetNames, Pageable page);
305+
306+
/**
307+
* @see DATAMONGO-1030
308+
*/
309+
PersonSummary findSummaryByLastname(String lastname);
305310
}
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)