Skip to content

Commit 72a17c3

Browse files
committed
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.
1 parent def31db commit 72a17c3

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,8 @@
2121
import java.lang.annotation.RetentionPolicy;
2222
import java.lang.annotation.Target;
2323

24-
import org.springframework.data.annotation.QueryAnnotation;
25-
2624
/**
2725
* Annotation to declare meta-information (execution time, cursor size, disk usage) for query methods.
28-
* <p>
29-
* Annotating a repository method with this annotation forces the method to be implemented as query method (i.e. using
30-
* this annotation on an overridden method from a base interface or fragment interface), similar to using
31-
* {@link Query @Query}.
3226
*
3327
* @author Christoph Strobl
3428
* @author Mark Paluch
@@ -37,7 +31,6 @@
3731
@Retention(RetentionPolicy.RUNTIME)
3832
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
3933
@Documented
40-
@QueryAnnotation
4134
public @interface Meta {
4235

4336
/**

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
6363
* @param lastname
6464
* @return
6565
*/
66+
@Meta
6667
List<Person> findByLastname(String lastname);
6768

6869
List<Person> findByLastnameStartsWith(String prefix);

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver;
3939
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
4040
import org.springframework.data.mongodb.core.query.TextCriteria;
41+
import org.springframework.data.mongodb.repository.Meta;
4142
import org.springframework.data.mongodb.repository.MongoRepository;
4243
import org.springframework.data.mongodb.repository.Person;
4344
import org.springframework.data.mongodb.repository.Person.Sex;
@@ -127,6 +128,12 @@ void doesNotDeriveFieldSpecForNormalDomainType() {
127128
assertThat(deriveQueryFromMethod("findPersonBy", new Object[0]).getFieldsObject()).isEmpty();
128129
}
129130

131+
@Test // GH-4852
132+
void appliesMetaToPartTreeQuery() {
133+
assertThat(deriveQueryFromMethod("findPersonBy", new Object[0]).getMeta()
134+
.getMaxTimeMsec()).isEqualTo(1234L);
135+
}
136+
130137
@Test // DATAMONGO-1345
131138
void restrictsQueryToFieldsRequiredForProjection() {
132139

@@ -193,7 +200,10 @@ private org.springframework.data.mongodb.core.query.Query deriveQueryFromMethod(
193200
PartTreeMongoQuery partTreeQuery = createQueryForMethod(method, types);
194201

195202
MongoParameterAccessor accessor = new MongoParametersParameterAccessor(partTreeQuery.getQueryMethod(), args);
196-
return partTreeQuery.createQuery(new ConvertingParameterAccessor(mongoOperationsMock.getConverter(), accessor));
203+
204+
org.springframework.data.mongodb.core.query.Query query = partTreeQuery.createQuery(new ConvertingParameterAccessor(mongoOperationsMock.getConverter(), accessor));
205+
partTreeQuery.applyQueryMetaAttributesWhenPresent(query);
206+
return query;
197207
}
198208

199209
private PartTreeMongoQuery createQueryForMethod(String methodName, Class<?>... paramTypes) {
@@ -230,6 +240,7 @@ interface Repo extends MongoRepository<Person, Long> {
230240
@Query(fields = "{ 'firstname }")
231241
Person findByAge(Integer age);
232242

243+
@Meta(maxExecutionTimeMs = 1234)
233244
Person findPersonBy();
234245

235246
PersonProjection findPersonProjectedBy();

0 commit comments

Comments
 (0)