DATAGRAPH-1199 - Add @ExistsQuery for convenience.

This commit is contained in:
Gerrit Meier
2019-02-18 20:10:05 +01:00
parent 55d6c92522
commit d53ec47b36
5 changed files with 68 additions and 2 deletions

View File

@@ -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 "";
}

View File

@@ -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 {

View File

@@ -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 {

View File

@@ -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")

View File

@@ -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<Restaurant, Long>
@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();
}