Browse Source

DATAMONGO-1752 - Fixed execution of repository query methods with closed projections.

We now skip the explicit definition of which type to read in case a closed projection is defined as return type.
pull/490/head
Oliver Gierke 9 years ago
parent
commit
97a0f3b635
  1. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java
  2. 16
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java
  3. 27
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonExcerpt.java
  4. 8
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java
  5. 9
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonSummary.java
  6. 25
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonSummaryDto.java

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

@ -86,8 +86,11 @@ public abstract class AbstractMongoQuery implements RepositoryQuery { @@ -86,8 +86,11 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
applyQueryMetaAttributesWhenPresent(query);
ResultProcessor processor = method.getResultProcessor().withDynamicProjection(accessor);
ReturnedType returnedType = processor.getReturnedType();
FindWithQuery<?> find = findOperationWithProjection.as(returnedType.getTypeToRead());
Class<?> typeToRead = processor.getReturnedType().getTypeToRead();
FindWithQuery<?> find = typeToRead == null //
? findOperationWithProjection //
: findOperationWithProjection.as(typeToRead);
MongoQueryExecution execution = getExecution(accessor, find);

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

@ -16,8 +16,10 @@ @@ -16,8 +16,10 @@
package org.springframework.data.mongodb.repository;
import static java.util.Arrays.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertThat;
import static org.springframework.data.geo.Metrics.*;
import java.util.ArrayList;
@ -928,7 +930,7 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -928,7 +930,7 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
@Test // DATAMONGO-1030
public void executesSingleEntityQueryWithProjectionCorrectly() {
PersonSummary result = repository.findSummaryByLastname("Beauford");
PersonSummaryDto result = repository.findSummaryByLastname("Beauford");
assertThat(result, is(notNullValue()));
assertThat(result.firstname, is("Carter"));
@ -1165,4 +1167,14 @@ public abstract class AbstractPersonRepositoryIntegrationTests { @@ -1165,4 +1167,14 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
assertThat(repository.countByThePersonsFirstname("Dave"), is(0L));
}
@Test // DATAMONGO-1752
public void readsOpenProjection() {
assertThat(repository.findOpenProjectionBy()).isNotEmpty();
}
@Test // DATAMONGO-1752
public void readsClosedProjection() {
assertThat(repository.findClosedProjectionBy()).isNotEmpty();
}
}

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

@ -0,0 +1,27 @@ @@ -0,0 +1,27 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.repository;
import org.springframework.beans.factory.annotation.Value;
/**
* @author Oliver Gierke
*/
public interface PersonExcerpt {
@Value("#{target.firstname + ' ' + target.lastname}")
String getFullName();
}

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

@ -286,7 +286,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query @@ -286,7 +286,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
Page<Person> findTop3ByLastnameStartingWith(String lastname, Pageable pageRequest);
// DATAMONGO-1030
PersonSummary findSummaryByLastname(String lastname);
PersonSummaryDto findSummaryByLastname(String lastname);
@Query("{ ?0 : ?1 }")
List<Person> findByKeyValue(String key, String value);
@ -325,4 +325,10 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query @@ -325,4 +325,10 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
*/
@DeleteQuery("{ 'firstname' : ?0 }") // DATAMONGO-1539
void deleteByThePersonsFirstname(String firstname);
// DATAMONGO-1752
Iterable<PersonExcerpt> findOpenProjectionBy();
// DATAMONGO-1752
Iterable<PersonSummary> findClosedProjectionBy();
}

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,8 +18,9 @@ package org.springframework.data.mongodb.repository; @@ -18,8 +18,9 @@ package org.springframework.data.mongodb.repository;
/**
* @author Oliver Gierke
*/
public class PersonSummary {
public interface PersonSummary {
String firstname;
String lastname;
String getFirstname();
String getLastname();
}

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

@ -0,0 +1,25 @@ @@ -0,0 +1,25 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.repository;
/**
* @author Oliver Gierke
*/
public class PersonSummaryDto {
String firstname;
String lastname;
}
Loading…
Cancel
Save