Skip to content

Commit 5650e35

Browse files
firatkucukodrotbohm
authored andcommitted
DATAMONGO-1539 - Introduce @CountQuery and @deleteQuery.
Introducing dedicated annotations for manually defined count and delete queries to avoid misconfiguration and generally simplifying the declaration. Original pull request: 416.
1 parent e5a41ad commit 5650e35

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2011-2016 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+
import java.lang.annotation.Documented;
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
import org.springframework.core.annotation.AliasFor;
24+
25+
/**
26+
* Annotation to declare finder count queries directly on repository methods. Both attributes allow using a placeholder
27+
* notation of {@code ?0}, {@code ?1} and so on.
28+
*
29+
* @see DATAMONGO-1539
30+
*
31+
* @author Fırat KÜÇÜK
32+
*/
33+
@Retention(RetentionPolicy.RUNTIME)
34+
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
35+
@Documented
36+
@Query(count = true)
37+
public @interface Count {
38+
39+
/**
40+
* Takes a MongoDB JSON string to define the actual query to be executed. This one will take precedence over the
41+
* method name then.
42+
*
43+
* Alias for {@link Query#value}.
44+
*
45+
* @return
46+
*/
47+
@AliasFor(annotation = Query.class)
48+
String value() default "";
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2011-2016 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+
import java.lang.annotation.Documented;
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
import org.springframework.core.annotation.AliasFor;
24+
25+
/**
26+
* Annotation to declare finder delete queries directly on repository methods. Both attributes allow using a placeholder
27+
* notation of {@code ?0}, {@code ?1} and so on.
28+
*
29+
* @see DATAMONGO-1539
30+
*
31+
* @author Fırat KÜÇÜK
32+
*/
33+
@Retention(RetentionPolicy.RUNTIME)
34+
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
35+
@Documented
36+
@Query(delete = true)
37+
public @interface Delete {
38+
39+
/**
40+
* Takes a MongoDB JSON string to define the actual query to be executed. This one will take precedence over the
41+
* method name then.
42+
*
43+
* Alias for {@link Query#value}.
44+
*
45+
* @return
46+
*/
47+
@AliasFor(annotation = Query.class)
48+
String value() default "";
49+
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
* @author Thomas Darimont
6565
* @author Christoph Strobl
6666
* @author Mark Paluch
67+
* @author Fırat KÜÇÜK
6768
*/
6869
@RunWith(SpringJUnit4ClassRunner.class)
6970
public abstract class AbstractPersonRepositoryIntegrationTests {
@@ -1307,4 +1308,25 @@ public void findsPersonsByFirstnameNotLike() throws Exception {
13071308
assertThat(result, not(hasItem(boyd)));
13081309
}
13091310

1311+
/**
1312+
* @see DATAMONGO-1539
1313+
*/
1314+
@Test
1315+
public void countsPersonsByFirstname() {
1316+
1317+
long result = repository.countByThePersonsFirstname("Dave");
1318+
assertThat(result, is(1L));
1319+
}
1320+
1321+
/**
1322+
* @see DATAMONGO-1539
1323+
*/
1324+
@Test
1325+
public void deletesPersonsByFirstname() {
1326+
1327+
repository.deleteByThePersonsFirstname("Dave");
1328+
1329+
long result = repository.countByThePersonsFirstname("Dave");
1330+
assertThat(result, is(0L));
1331+
}
13101332
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @author Oliver Gierke
4343
* @author Thomas Darimont
4444
* @author Christoph Strobl
45+
* @author Fırat KÜÇÜK
4546
*/
4647
public interface PersonRepository extends MongoRepository<Person, String>, QueryDslPredicateExecutor<Person> {
4748

@@ -367,4 +368,27 @@ Page<Person> findByCustomQueryLastnameAndAddressStreetInList(String lastname, Li
367368
*/
368369
@Query("{ firstname : :#{#firstname}}")
369370
List<Person> findWithSpelByFirstnameForSpELExpressionWithParameterVariableOnly(@Param("firstname") String firstname);
371+
372+
/**
373+
* Returns the count of {@link Person} with the given firstname. Uses {@link Count} annotation to define the query
374+
* to be executed.
375+
*
376+
* @see DATAMONGO-1539
377+
*
378+
* @param firstname
379+
* @return
380+
*/
381+
@Count(value = "{ 'firstname' : ?0 }")
382+
long countByThePersonsFirstname(String firstname);
383+
384+
/**
385+
* Deletes {@link Person} entities with the given firstname. Uses {@link Delete} annotation to define the query
386+
* to be executed.
387+
*
388+
* @see DATAMONGO-1539
389+
*
390+
* @param firstname
391+
*/
392+
@Delete(value = "{ 'firstname' : ?0 }")
393+
void deleteByThePersonsFirstname(String firstname);
370394
}

0 commit comments

Comments
 (0)