Browse Source

DATAMONGO-871 - Add support for arrays as query method return types.

Changed AbstractMongoQuery to potentially convert all query execution results using the DefaultConversionService in case the query result doesn't match the expected return value.

This allows arrays to be returned for collection queries as the conversion service cam transparently convert between collections and arrays.
pull/144/head
Oliver Gierke 12 years ago
parent
commit
4f0b2d66a5
  1. 6
      pom.xml
  2. 14
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java
  3. 14
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/AbstractPersonRepositoryIntegrationTests.java
  4. 8
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java

6
pom.xml

@ -29,7 +29,7 @@
<properties> <properties>
<project.type>multi</project.type> <project.type>multi</project.type>
<dist.id>spring-data-mongodb</dist.id> <dist.id>spring-data-mongodb</dist.id>
<springdata.commons>1.6.4.RELEASE</springdata.commons> <springdata.commons>1.6.5.BUILD-SNAPSHOT</springdata.commons>
<mongo>2.10.1</mongo> <mongo>2.10.1</mongo>
</properties> </properties>
@ -113,8 +113,8 @@
<repositories> <repositories>
<repository> <repository>
<id>spring-lib-release</id> <id>spring-lib-snapshot</id>
<url>http://repo.spring.io/libs-release</url> <url>http://repo.spring.io/libs-snapshot</url>
</repository> </repository>
</repositories> </repositories>

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2013 the original author or authors. * Copyright 2010-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -79,22 +79,24 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
MongoParameterAccessor accessor = new MongoParametersParameterAccessor(method, parameters); MongoParameterAccessor accessor = new MongoParametersParameterAccessor(method, parameters);
Query query = createQuery(new ConvertingParameterAccessor(operations.getConverter(), accessor)); Query query = createQuery(new ConvertingParameterAccessor(operations.getConverter(), accessor));
Object result = null;
if (method.isGeoNearQuery() && method.isPageQuery()) { if (method.isGeoNearQuery() && method.isPageQuery()) {
MongoParameterAccessor countAccessor = new MongoParametersParameterAccessor(method, parameters); MongoParameterAccessor countAccessor = new MongoParametersParameterAccessor(method, parameters);
Query countQuery = createCountQuery(new ConvertingParameterAccessor(operations.getConverter(), countAccessor)); Query countQuery = createCountQuery(new ConvertingParameterAccessor(operations.getConverter(), countAccessor));
return new GeoNearExecution(accessor).execute(query, countQuery); result = new GeoNearExecution(accessor).execute(query, countQuery);
} else if (method.isGeoNearQuery()) { } else if (method.isGeoNearQuery()) {
return new GeoNearExecution(accessor).execute(query); return new GeoNearExecution(accessor).execute(query);
} else if (method.isCollectionQuery()) { } else if (method.isCollectionQuery()) {
return new CollectionExecution(accessor.getPageable()).execute(query); result = new CollectionExecution(accessor.getPageable()).execute(query);
} else if (method.isPageQuery()) { } else if (method.isPageQuery()) {
return new PagedExecution(accessor.getPageable()).execute(query); result = new PagedExecution(accessor.getPageable()).execute(query);
} else {
result = new SingleEntityExecution(isCountQuery()).execute(query);
} }
Object result = new SingleEntityExecution(isCountQuery()).execute(query);
if (result == null) { if (result == null) {
return result; return result;
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2011-2013 the original author or authors. * Copyright 2011-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -680,4 +680,16 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
assertThat(results.isLastPage(), is(true)); assertThat(results.isLastPage(), is(true));
assertThat(results.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS)); assertThat(results.getAverageDistance().getMetric(), is((Metric) Metrics.KILOMETERS));
} }
/**
* @see DATAMONGO-871
*/
@Test
public void findsPersonsByFirstnameAsArray() {
Person[] result = repository.findByThePersonsFirstnameAsArray("Leroi");
assertThat(result, is(arrayWithSize(1)));
assertThat(result, is(arrayContaining(leroi)));
}
} }

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2013 the original author or authors. * Copyright 2010-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -69,6 +69,12 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
@Query(value = "{ 'firstname' : ?0 }", fields = "{ 'firstname': 1, 'lastname': 1}") @Query(value = "{ 'firstname' : ?0 }", fields = "{ 'firstname': 1, 'lastname': 1}")
List<Person> findByThePersonsFirstname(String firstname); List<Person> findByThePersonsFirstname(String firstname);
/**
* @see DATAMONGO-871
*/
@Query(value = "{ 'firstname' : ?0 }")
Person[] findByThePersonsFirstnameAsArray(String firstname);
/** /**
* Returns all {@link Person}s with a firstname matching the given one (*-wildcard supported). * Returns all {@link Person}s with a firstname matching the given one (*-wildcard supported).
* *

Loading…
Cancel
Save