Browse Source

Allow Meta annotation usage for derived queries.

We now allow applying Meta to derived queries without turning a derived query method into a annotated one.

Original pull request #4854
Closes #4852
issue/aot-doc
Mark Paluch 3 months ago committed by Jens Schauder
parent
commit
f6fadfd3e5
No known key found for this signature in database
GPG Key ID: 2BE5D185CD2A1CE6
  1. 7
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/Meta.java
  2. 1
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/PersonRepository.java
  3. 13
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQueryUnitTests.java

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

@ -21,14 +21,8 @@ import java.lang.annotation.Retention; @@ -21,14 +21,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.data.annotation.QueryAnnotation;
/**
* Annotation to declare meta-information (execution time, cursor size, disk usage) for query methods.
* <p>
* Annotating a repository method with this annotation forces the method to be implemented as query method (i.e. using
* this annotation on an overridden method from a base interface or fragment interface), similar to using
* {@link Query @Query}.
*
* @author Christoph Strobl
* @author Mark Paluch
@ -37,7 +31,6 @@ import org.springframework.data.annotation.QueryAnnotation; @@ -37,7 +31,6 @@ import org.springframework.data.annotation.QueryAnnotation;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Documented
@QueryAnnotation
public @interface Meta {
/**

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

@ -63,6 +63,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query @@ -63,6 +63,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
* @param lastname
* @return
*/
@Meta
List<Person> findByLastname(String lastname);
List<Person> findByLastnameStartsWith(String prefix);

13
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/PartTreeMongoQueryUnitTests.java

@ -38,6 +38,7 @@ import org.springframework.data.mongodb.core.convert.MongoConverter; @@ -38,6 +38,7 @@ import org.springframework.data.mongodb.core.convert.MongoConverter;
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.query.TextCriteria;
import org.springframework.data.mongodb.repository.Meta;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Person;
import org.springframework.data.mongodb.repository.Person.Sex;
@ -127,6 +128,12 @@ class PartTreeMongoQueryUnitTests { @@ -127,6 +128,12 @@ class PartTreeMongoQueryUnitTests {
assertThat(deriveQueryFromMethod("findPersonBy", new Object[0]).getFieldsObject()).isEmpty();
}
@Test // GH-4852
void appliesMetaToPartTreeQuery() {
assertThat(deriveQueryFromMethod("findPersonBy", new Object[0]).getMeta()
.getMaxTimeMsec()).isEqualTo(1234L);
}
@Test // DATAMONGO-1345
void restrictsQueryToFieldsRequiredForProjection() {
@ -193,7 +200,10 @@ class PartTreeMongoQueryUnitTests { @@ -193,7 +200,10 @@ class PartTreeMongoQueryUnitTests {
PartTreeMongoQuery partTreeQuery = createQueryForMethod(method, types);
MongoParameterAccessor accessor = new MongoParametersParameterAccessor(partTreeQuery.getQueryMethod(), args);
return partTreeQuery.createQuery(new ConvertingParameterAccessor(mongoOperationsMock.getConverter(), accessor));
org.springframework.data.mongodb.core.query.Query query = partTreeQuery.createQuery(new ConvertingParameterAccessor(mongoOperationsMock.getConverter(), accessor));
partTreeQuery.applyQueryMetaAttributesWhenPresent(query);
return query;
}
private PartTreeMongoQuery createQueryForMethod(String methodName, Class<?>... paramTypes) {
@ -230,6 +240,7 @@ class PartTreeMongoQueryUnitTests { @@ -230,6 +240,7 @@ class PartTreeMongoQueryUnitTests {
@Query(fields = "{ 'firstname }")
Person findByAge(Integer age);
@Meta(maxExecutionTimeMs = 1234)
Person findPersonBy();
PersonProjection findPersonProjectedBy();

Loading…
Cancel
Save