DATAGRAPH-392 - Add ability to override CRUD methods with @Query.

Annotated @Query annotation with @QueryAnnotation to mark it as a custom query annotation. Added appropriate test cases.

Original pull request: #137.
This commit is contained in:
Thomas Darimont
2013-09-24 10:47:42 +02:00
committed by Oliver Gierke
parent f430b448b8
commit a30fc18ed2
3 changed files with 115 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2011 the original author or authors.
* Copyright 2011-2013 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.
@@ -21,6 +21,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.data.annotation.QueryAnnotation;
/**
* Field that provides access to an iterator which is created by applying the provided query starting at the current
* entity-node. The result elements are automatically converted to appropriate element
@@ -30,10 +32,12 @@ import java.lang.annotation.Target;
* Iterable<Person> friends;
* </pre>
* @author Michael Hunger
* @author Thomas Darimont
* @since 15.09.2010
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.METHOD})
@QueryAnnotation
public @interface Query {
/**
* @return Query to be executed {self} will be provided by the node-id of the current entity other parameters (e.g. {name}) by the given named params

View File

@@ -0,0 +1,44 @@
/**
* Copyright 2013 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.repository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.conversion.EndResult;
import org.springframework.data.neo4j.model.Person;
/**
* @author Thomas Darimont
*/
public interface RedeclaringRepositoryMethodsRepository extends GraphRepository<Person> {
/**
* Should not find any persons at all.
*/
@Query("START n=node(*) where HAS(n.name) AND n.name='Bubu' return n")
EndResult<Person> findAll();
/**
* Should only find persons with the name 'Oliver'.
*
* @param page
* @return
*/
@Query("START n=node(*) where HAS(n.name) AND n.name='Oliver' return n")
Page<Person> findAll(Pageable page);
}

View File

@@ -0,0 +1,66 @@
/**
* Copyright 2013 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.repository;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.neo4j.conversion.EndResult;
import org.springframework.data.neo4j.model.Person;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Thomas Darimont
*/
@Transactional
public class RedeclaringRepositoryMethodsTests extends AbstractEntityBasedGraphRepositoryTests {
@Autowired RedeclaringRepositoryMethodsRepository repository;
/**
* @see DATAGRAPH-392
*/
@Test
public void adjustedWellKnownPagedFindAllMethodShouldReturnOnlyTheUserWithFirstnameOliver() {
Person ollie = repository.save(new Person("Oliver", 30));
repository.save(new Person("Thomas", 30));
Page<Person> page = repository.findAll(new PageRequest(0, 2));
assertThat(page.getNumberOfElements(), is(1));
assertThat(page.getContent().get(0).getName(), is(ollie.getName()));
}
/**
* @see DATAGRAPH-392
*/
@Test
public void adjustedWllKnownFindAllMethodShouldReturnAnEmptyList() {
repository.save(new Person("Oliver", 30));
repository.save(new Person("Thomas", 30));
EndResult<Person> result = repository.findAll();
assertThat(result.iterator().hasNext(), is(false));
}
}