From d53ec47b369ac8cb0bfeb10b19c56a4279148971 Mon Sep 17 00:00:00 2001 From: Gerrit Meier Date: Mon, 18 Feb 2019 20:10:05 +0100 Subject: [PATCH] DATAGRAPH-1199 - Add @ExistsQuery for convenience. --- .../data/neo4j/annotation/ExistsQuery.java | 46 +++++++++++++++++++ .../data/neo4j/annotation/Query.java | 2 +- .../repository/query/GraphQueryMethod.java | 2 +- .../examples/restaurants/RestaurantTests.java | 16 +++++++ .../repo/RestaurantRepository.java | 4 ++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 spring-data-neo4j/src/main/java/org/springframework/data/neo4j/annotation/ExistsQuery.java diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/annotation/ExistsQuery.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/annotation/ExistsQuery.java new file mode 100644 index 000000000..dda03e5ba --- /dev/null +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/annotation/ExistsQuery.java @@ -0,0 +1,46 @@ +/* + * Copyright 2011-2019 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.neo4j.annotation; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.core.annotation.AliasFor; + +/** + * Annotation to declare a {@link Query} with a value containing an exists custom query. The result will then be used for + * boolean projection. + * + * @author Gerrit Meier + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE }) +@Documented +@Query(exists = true) +public @interface ExistsQuery { + + /** + * The cypher query. + * + * @see Query#value() + * @since 5.2 + */ + @AliasFor(annotation = Query.class) + String value() default ""; +} diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/annotation/Query.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/annotation/Query.java index e988c28e4..ee8a65817 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/annotation/Query.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/annotation/Query.java @@ -31,7 +31,7 @@ import org.springframework.data.annotation.QueryAnnotation; * @author Gerrit Meier */ @Retention(RetentionPolicy.RUNTIME) -@Target(ElementType.METHOD) +@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) @QueryAnnotation @Documented public @interface Query { diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/query/GraphQueryMethod.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/query/GraphQueryMethod.java index 7747889e1..efb1a84fb 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/query/GraphQueryMethod.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/repository/query/GraphQueryMethod.java @@ -50,7 +50,7 @@ public class GraphQueryMethod extends QueryMethod { super(method, metadata, factory); this.method = method; - this.queryAnnotation = method.getAnnotation(Query.class); + this.queryAnnotation = AnnotatedElementUtils.findMergedAnnotation(method, Query.class); if (this.queryAnnotation != null) { this.isExistsQuery = queryAnnotation.exists(); } else { diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/examples/restaurants/RestaurantTests.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/examples/restaurants/RestaurantTests.java index f3eb73559..6e009021d 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/examples/restaurants/RestaurantTests.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/examples/restaurants/RestaurantTests.java @@ -521,6 +521,22 @@ public class RestaurantTests { assertFalse(restaurantRepository.existenceOfAGoodRestaurant()); } + @Test // DATAGRAPH-1199 + public void shouldCheckExistenceForExistsQueryMethod() { + Restaurant restaurant = new Restaurant("R1", "good"); + restaurantRepository.save(restaurant); + + assertTrue(restaurantRepository.existenceOfAGoodRestaurantWithExistsQuery()); + } + + @Test // DATAGRAPH-1199 + public void shouldCheckNonExistenceForExistsQueryMethod() { + Restaurant restaurant = new Restaurant("R1", "bad"); + restaurantRepository.save(restaurant); + + assertFalse(restaurantRepository.existenceOfAGoodRestaurantWithExistsQuery()); + } + @Configuration @Neo4jIntegrationTest(domainPackages = "org.springframework.data.neo4j.examples.restaurants.domain", repositoryPackages = "org.springframework.data.neo4j.examples.restaurants.repo") diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/examples/restaurants/repo/RestaurantRepository.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/examples/restaurants/repo/RestaurantRepository.java index b6e2f685a..b6c3dab71 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/examples/restaurants/repo/RestaurantRepository.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/examples/restaurants/repo/RestaurantRepository.java @@ -20,6 +20,7 @@ import java.util.List; import org.springframework.data.geo.Distance; import org.springframework.data.geo.Point; +import org.springframework.data.neo4j.annotation.ExistsQuery; import org.springframework.data.neo4j.annotation.Query; import org.springframework.data.neo4j.examples.restaurants.domain.Restaurant; import org.springframework.data.neo4j.repository.Neo4jRepository; @@ -85,4 +86,7 @@ public interface RestaurantRepository extends Neo4jRepository @Query(value = "MATCH (r:Restaurant) WHERE r.description = 'good' return r", exists = true) boolean existenceOfAGoodRestaurant(); + + @ExistsQuery("MATCH (r:Restaurant) WHERE r.description = 'good' return r") + boolean existenceOfAGoodRestaurantWithExistsQuery(); }