renamed base repository interfaces

This commit is contained in:
Michael Hunger
2011-04-07 13:08:54 +02:00
parent d506b73211
commit b27980c242
23 changed files with 55 additions and 58 deletions

View File

@@ -95,11 +95,13 @@ For more detailed questions, use the [forum](http://forum.springsource.org/forum
* Create a repository or service to perform typical operations on your entities. The FinderFactory and Finder helper classes make searching easy for common use cases. The complete functionality is covered in the [reference manual](http://static.springsource.org/spring-data/data-graph/docs/current/reference/html/#programming-model).
public interface WorldRepository extends GraphRepository<World>, NamedIndexRepository<World> {}
@Repository
public class WorldRepository {
public class WorldRepositoryImpl implements WorldRepositoryExtension {
@Autowired
private FinderFactory graphRepositoryFactory;
private WorldRepository worldRepository;
@Transactional
public Collection<World> makeSomeWorlds() {
@@ -117,31 +119,28 @@ For more detailed questions, use the [forum](http://forum.springsource.org/forum
return new World(name,moons).persist();
}
private NodeFinder<World> graphRepository() {
return graphRepositoryFactory.createNodeEntityFinder(World.class);
}
public World findWorldIdentifiedBy( long id ) {
return graphRepository().findById( id );
return worldRepository.findById( id );
}
public Iterable<World> findAllWorlds() {
return graphRepository().findAll();
return worldRepository.findAll();
}
public long countWorlds() {
return graphRepository().count();
return worldRepository.count();
}
public World findWorldNamed( String name ) {
return graphRepository().findByPropertyValue( null, "name", name );
return worldRepository.findByPropertyValue( "name", name );
}
public World findWorldWithMoons( long moonCount ) {
return graphRepository().findByPropertyValue( "moons", "moons", moonCount );
return worldRepository.findByPropertyValue( "moons", "moons", moonCount );
}
public Iterable<World> findWorldsWithMoons( int moonCount ) {
return graphRepository().findAllByPropertyValue( "moons", "moons", moonCount );
return worldRepository.findAllByPropertyValue( "moons", "moons", moonCount );
}
}

View File

@@ -24,7 +24,7 @@ package org.springframework.data.graph.neo4j.config;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.data.graph.neo4j.repository.CRUDGraphRepository;
import org.springframework.data.graph.neo4j.repository.CRUDRepository;
import org.springframework.data.graph.neo4j.repository.GraphRepositoryFactoryBean;
import org.springframework.data.repository.config.*;
import org.springframework.util.StringUtils;
@@ -76,7 +76,7 @@ public class DataGraphNamespaceHandler extends NamespaceHandlerSupport {
@Override
public Class<?> getRepositoryBaseInterface() {
return CRUDGraphRepository.class;
return CRUDRepository.class;
}
public String getGraphDatabaseContextRef() {

View File

@@ -86,7 +86,7 @@ public class TraversalFieldAccessorFactory implements FieldAccessorFactory<NodeB
@Override
public Object getValue(final NodeBacked nodeBacked) {
final GraphRepository<? extends NodeBacked> finder = graphRepositoryFactory.createNodeEntityRepository(target);
final GraphRepository<? extends NodeBacked> finder = graphRepositoryFactory.createGraphRepository(target);
final TraversalDescription traversalDescription = fieldTraversalDescriptionBuilder.build(nodeBacked,field,params);
return doReturn(finder.findAllByTraversal(nodeBacked, traversalDescription));
}

View File

@@ -43,7 +43,7 @@ import java.util.List;
* @param <S> Type of backing state, either Node or Relationship
*/
@org.springframework.stereotype.Repository
public abstract class AbstractGraphRepository<S extends PropertyContainer, T extends GraphBacked<S>> implements GraphRepository<T>, CRUDGraphRepository<T>, NamedIndexQueryExecutor<T> {
public abstract class AbstractGraphRepository<S extends PropertyContainer, T extends GraphBacked<S>> implements GraphRepository<T>, NamedIndexRepository<T> {
public static final ClosableIterable EMPTY_CLOSABLE_ITERABLE = new ClosableIterable() {
@Override
public void close() {

View File

@@ -29,7 +29,7 @@ import org.springframework.transaction.annotation.Transactional;
* CRUD interface for graph repositories, used as base repository for crud operations
*/
@NoRepositoryBean
public interface CRUDGraphRepository<T extends GraphBacked<?>> extends PagingAndSortingRepository<T, Long> {
public interface CRUDRepository<T extends GraphBacked<?>> extends PagingAndSortingRepository<T, Long> {
/**
* persists an entity by forwarding to entity.persist()

View File

@@ -16,6 +16,7 @@
package org.springframework.data.graph.neo4j.repository;
import org.springframework.data.graph.core.GraphBacked;
import org.springframework.data.graph.core.NodeBacked;
import org.springframework.data.graph.core.RelationshipBacked;
import org.springframework.data.graph.neo4j.support.GraphDatabaseContext;
@@ -31,12 +32,10 @@ public class DirectGraphRepositoryFactory {
this.graphDatabaseContext = graphDatabaseContext;
}
public <T extends NodeBacked> GraphRepository<T> createNodeEntityRepository(Class<T> clazz) {
return new NodeGraphRepository<T>(clazz, graphDatabaseContext);
@SuppressWarnings({"unchecked"})
public <T extends GraphBacked<?>> GraphRepository<T> createGraphRepository(Class<T> clazz) {
if (NodeBacked.class.isAssignableFrom(clazz)) return new NodeGraphRepository(clazz, graphDatabaseContext);
if (RelationshipBacked.class.isAssignableFrom(clazz)) return new RelationshipGraphRepository(clazz, graphDatabaseContext);
throw new IllegalArgumentException("Can't create graph repository for non graph entity of type "+clazz);
}
public <T extends RelationshipBacked> GraphRepository<T> createRelationshipEntityRepository(Class<T> clazz) {
return new RelationshipGraphRepository<T>(clazz, graphDatabaseContext);
}
}

View File

@@ -24,6 +24,6 @@ import org.springframework.data.repository.NoRepositoryBean;
* @since 12.01.11
*/
@NoRepositoryBean
public interface GraphRepository<T extends GraphBacked<?>> extends CRUDGraphRepository<T>, IndexQueryExecutor<T>, TraversalQueryExecutor<T> {
public interface GraphRepository<T extends GraphBacked<?>> extends CRUDRepository<T>, IndexRepository<T>, TraversalRepository<T> {
}

View File

@@ -27,7 +27,7 @@ import org.springframework.util.Assert;
* @author mh
* @since 28.03.11
*/
public class GraphRepositoryFactoryBean<S extends PropertyContainer, R extends CRUDGraphRepository<T>, T extends GraphBacked<S>>
public class GraphRepositoryFactoryBean<S extends PropertyContainer, R extends CRUDRepository<T>, T extends GraphBacked<S>>
extends TransactionalRepositoryFactoryBeanSupport<R, T, Long> {
private GraphDatabaseContext graphDatabaseContext;

View File

@@ -23,7 +23,7 @@ import org.springframework.data.graph.core.GraphBacked;
* @author mh
* @since 29.03.11
*/
public interface IndexQueryExecutor<T extends GraphBacked<?>> {
public interface IndexRepository<T extends GraphBacked<?>> {
T findByPropertyValue(String property, Object value);
ClosableIterable<T> findAllByPropertyValue(String property, Object value);

View File

@@ -23,7 +23,7 @@ import org.springframework.data.graph.core.GraphBacked;
* @author mh
* @since 29.03.11
*/
public interface NamedIndexQueryExecutor<T extends GraphBacked<?>> {
public interface NamedIndexRepository<T extends GraphBacked<?>> {
T findByPropertyValue(String indexName, String property, Object value);
ClosableIterable<T> findAllByPropertyValue(String indexName, String property, Object value);

View File

@@ -24,7 +24,7 @@ import org.springframework.data.graph.core.NodeBacked;
* @author mh
* @since 29.03.11
*/
public interface TraversalQueryExecutor<T extends GraphBacked<?>> {
public interface TraversalRepository<T extends GraphBacked<?>> {
/**
* Traversal based finder that returns a lazy Iterable over the traversal results
*

View File

@@ -16,12 +16,12 @@
package org.springframework.data.graph.neo4j;
import org.springframework.data.graph.neo4j.repository.NamedIndexQueryExecutor;
import org.springframework.data.graph.neo4j.repository.NamedIndexRepository;
import org.springframework.data.graph.neo4j.repository.GraphRepository;
/**
* @author mh
* @since 29.03.11
*/
public interface GroupRepository extends GraphRepository<Group>, NamedIndexQueryExecutor<Group> {
public interface GroupRepository extends GraphRepository<Group>, NamedIndexRepository<Group> {
}

View File

@@ -16,12 +16,12 @@
package org.springframework.data.graph.neo4j;
import org.springframework.data.graph.neo4j.repository.NamedIndexQueryExecutor;
import org.springframework.data.graph.neo4j.repository.NamedIndexRepository;
import org.springframework.data.graph.neo4j.repository.GraphRepository;
/**
* @author mh
* @since 29.03.11
*/
public interface PersonRepository extends GraphRepository<Person>, NamedIndexQueryExecutor<Person> {
public interface PersonRepository extends GraphRepository<Person>, NamedIndexRepository<Person> {
}

View File

@@ -69,8 +69,8 @@ public class IndexTest {
@Before
public void setUp() throws Exception {
groupFinder = graphRepositoryFactory.createNodeEntityRepository(Group.class);
personFinder = graphRepositoryFactory.createNodeEntityRepository(Person.class);
groupFinder = graphRepositoryFactory.createGraphRepository(Group.class);
personFinder = graphRepositoryFactory.createGraphRepository(Person.class);
}
@BeforeTransaction
@@ -85,7 +85,7 @@ public class IndexTest {
Person p2 = persistedPerson(NAME_VALUE2, 25);
Friendship friendship = p.knows(p2);
friendship.setYears(1);
GraphRepository<Friendship> friendshipFinder = graphRepositoryFactory.createRelationshipEntityRepository(Friendship.class);
GraphRepository<Friendship> friendshipFinder = graphRepositoryFactory.createGraphRepository(Friendship.class);
assertEquals(friendship, friendshipFinder.findByPropertyValue("Friendship.years", 1));
}

View File

@@ -195,7 +195,7 @@ public class ModificationOutsideOfTransactionTest
@Test
public void testFindOutsideTransaction()
{
final GraphRepository<Person> finder = graphRepositoryFactory.createNodeEntityRepository(Person.class);
final GraphRepository<Person> finder = graphRepositoryFactory.createGraphRepository(Person.class);
assertEquals( false, finder.findAll().iterator().hasNext() );
}

View File

@@ -71,7 +71,7 @@ import static org.springframework.data.graph.neo4j.Person.persistedPerson;
Person person2 = graphDatabaseContext.createEntityFromState(node,Person.class);
assertEquals("Rod", person2.getName());
GraphRepository<Person> finder = graphRepositoryFactory.createNodeEntityRepository(Person.class);
GraphRepository<Person> finder = graphRepositoryFactory.createGraphRepository(Person.class);
Person found = finder.findOne(nodeId);
assertEquals("Rod", found.getName());
}

View File

@@ -187,7 +187,7 @@ public class SubReferenceNodeTypeRepresentationStrategyTest {
public void testInstantiateConcreteClassWithFinder() {
log.debug("testInstantiateConcreteClassWithFinder");
Volvo v=new Volvo().persist();
GraphRepository<Car> finder = graphRepositoryFactory.createNodeEntityRepository(Car.class);
GraphRepository<Car> finder = graphRepositoryFactory.createGraphRepository(Car.class);
assertEquals("Wrong concrete class.", Volvo.class, finder.findAll().iterator().next().getClass());
}
@@ -199,16 +199,16 @@ public class SubReferenceNodeTypeRepresentationStrategyTest {
log.warn("Created volvo");
new Toyota().persist();
log.warn("Created volvo");
assertEquals("Wrong count for Volvo.", (Long)1L, graphRepositoryFactory.createNodeEntityRepository(Volvo.class).count());
assertEquals("Wrong count for Toyota.", (Long)1L, graphRepositoryFactory.createNodeEntityRepository(Toyota.class).count());
assertEquals("Wrong count for Car.", (Long)2L, graphRepositoryFactory.createNodeEntityRepository(Car.class).count());
assertEquals("Wrong count for Volvo.", (Long)1L, graphRepositoryFactory.createGraphRepository(Volvo.class).count());
assertEquals("Wrong count for Toyota.", (Long)1L, graphRepositoryFactory.createGraphRepository(Toyota.class).count());
assertEquals("Wrong count for Car.", (Long)2L, graphRepositoryFactory.createGraphRepository(Car.class).count());
}
@Test
@Transactional
public void testCountClasses() {
persistedPerson("Michael", 36);
persistedPerson("David", 25);
assertEquals("Wrong Person instance count.", (Long)2L, graphRepositoryFactory.createNodeEntityRepository(Person.class).count());
assertEquals("Wrong Person instance count.", (Long)2L, graphRepositoryFactory.createGraphRepository(Person.class).count());
}

View File

@@ -114,7 +114,7 @@ public class TraversalTest {
@Test
@Transactional
public void testTraverseFromGroupToPeopleWithFinder() {
final GraphRepository<Person> finder = graphRepositoryFactory.createNodeEntityRepository(Person.class);
final GraphRepository<Person> finder = graphRepositoryFactory.createGraphRepository(Person.class);
Person p = persistedPerson("Michael", 35);
Group group = new Group().persist();
group.setName("dev");

View File

@@ -44,13 +44,13 @@ class Person {
}
GraphRepository<Person> graphRepository = graphRepositoryFactory.createNodeEntityRepository(Person.class);
GraphRepository<Person> graphRepository = graphRepositoryFactory.createGraphRepository(Person.class);
// exact graphRepository
Person mark = graphRepository.findByProperyValue("people","name","mark");
// numeric range queries
for (Person middleAgedDeveloper : graphRepository.findAllByRange(null, "age", 20, 40)) {
for (Person middleAgedDeveloper : graphRepository.findAllByRange( "age", 20, 40)) {
Developer developer=middleAgedDeveloper.projectTo(Developer.class);
}
]]></programlisting>
@@ -76,7 +76,7 @@ class Person {
String name;
}
GraphRepository<Person> graphRepository = graphRepositoryFactory.createNodeEntityRepository(Person.class);
GraphRepository<Person> graphRepository = graphRepositoryFactory.createGraphRepository(Person.class);
// exact graphRepository
Person mark = graphRepository.findAllByQuery("people-search","name","ma*");

View File

@@ -94,7 +94,7 @@
<example>
<title>Using GraphRepositories</title>
<programlisting language="java"><![CDATA[
GraphRepository<Person> graphRepository = graphRepositoryFactory.createNodeEntityRepository(Person.class);
GraphRepository<Person> graphRepository = graphRepositoryFactory.createGraphRepository(Person.class);
Person michael = graphRepository.save(new Person("Michael",36));
@@ -102,13 +102,13 @@ Person dave=graphRepository.findOne(123);
Long numberOfPeople = graphRepository.count();
Person mark = graphRepository.findByPropertyValue(null,"name", "mark");
Person mark = graphRepository.findByPropertyValue("name", "mark");
Iterable<Person> devs = graphRepository.findAllByProperyValue(null, "occupation","developer");
Iterable<Person> devs = graphRepository.findAllByProperyValue("occupation","developer");
Iterable<Person> middleAgedPeople = graphRepository.findAllByRange(null, "age",20,40);
Iterable<Person> middleAgedPeople = graphRepository.findAllByRange("age",20,40);
Iterable<Person> aTeam = graphRepository.findAllByQuery(null, "name","A*");
Iterable<Person> aTeam = graphRepository.findAllByQuery("name","A*");
Iterable<Person> davesFriends = graphRepository.findAllByTraversal(dave,
Traversal.description().pruneAfterDepth(1)
@@ -155,9 +155,9 @@ PersonRepository personRepository;
Person dave=personRepository.findOne(123);
Iterable<Person> devs = personRepository.findAllByProperyValue(null, "occupation","developer");
Iterable<Person> devs = personRepository.findAllByProperyValue("occupation","developer");
Iterable<Person> aTeam = graphRepository.findAllByQuery(null, "name","A*");
Iterable<Person> aTeam = graphRepository.findAllByQuery( "name","A*");
Iterable<Person> friends = personRepository.findFriends(dave);
]]></programlisting>

View File

@@ -24,9 +24,8 @@
@Test public void persistedMovieShouldBeRetrievableFromGraphDb() {
int id = 1;
Movie forrestGump = new Movie(id, "Forrest Gump", 1994).persist();
GraphRepository<Movie> movieRepository = graphRepositoryFactory.createNodeEntityRepository(Movie.class);
// REMINDER, the "null" stands for an optional index name
Movie retrievedMovie = movieRepository.findByPropertyValue(null, "id", id);
GraphRepository<Movie> movieRepository = graphRepositoryFactory.createGraphRepository(Movie.class);
Movie retrievedMovie = movieRepository.findByPropertyValue("id", id);
assertEqual("retrieved movie matches persisted one", forrestGump, retrievedMovie);
assertEqual("retrieved movie title matches", "Forrest Gump", retrievedMovie.getTitle());
}

View File

@@ -33,7 +33,7 @@ public class CineastsRepostory {
@Autowired MovieRepository movieRepository;
public Movie getMovie(int id) {
return movieRepository.findByPropertyValue(null,"id", id);
return movieRepository.findByPropertyValue("id", id);
}
}
]]></programlisting>

View File

@@ -103,7 +103,7 @@ public class CineastsUserDetailsService implements UserDetailsService, Initializ
}
public User findUser(String login) {
return userRepository.findByPropertyValue(null,"login",login);
return userRepository.findByPropertyValue("login",login);
}
public User getUserFromSession() {
SecurityContext context = SecurityContextHolder.getContext();