From 789a44d2bbc8446cfc66d74613cd32585bd6a23b Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Sat, 2 Apr 2011 00:01:23 +0200 Subject: [PATCH] updated documentation, renamed EntityInstantiators --- ...iator.java => NodeEntityInstantiator.java} | 2 +- ...ava => PartialNodeEntityInstantiator.java} | 6 +- ...va => RelationshipEntityInstantiator.java} | 2 +- src/docbkx/reference/neo4j-intro.xml | 8 - .../reference/programming-model/finders.xml | 76 -------- .../programming-model/repositories.xml | 168 ++++++++++++++++++ 6 files changed, 173 insertions(+), 89 deletions(-) rename spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/{Neo4jConstructorGraphEntityInstantiator.java => NodeEntityInstantiator.java} (91%) rename spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/{PartialNeo4jEntityInstantiator.java => PartialNodeEntityInstantiator.java} (89%) rename spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/relationship/{ConstructorBypassingGraphRelationshipInstantiator.java => RelationshipEntityInstantiator.java} (92%) delete mode 100644 src/docbkx/reference/neo4j-intro.xml delete mode 100644 src/docbkx/reference/programming-model/finders.xml create mode 100644 src/docbkx/reference/programming-model/repositories.xml diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/Neo4jConstructorGraphEntityInstantiator.java b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/NodeEntityInstantiator.java similarity index 91% rename from spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/Neo4jConstructorGraphEntityInstantiator.java rename to spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/NodeEntityInstantiator.java index 88e395cd6..de9abafba 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/Neo4jConstructorGraphEntityInstantiator.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/NodeEntityInstantiator.java @@ -27,7 +27,7 @@ import org.springframework.data.persistence.AbstractConstructorEntityInstantiato * * @author Rod Johnson */ -public class Neo4jConstructorGraphEntityInstantiator extends AbstractConstructorEntityInstantiator{ +public class NodeEntityInstantiator extends AbstractConstructorEntityInstantiator{ @Override protected void setState(NodeBacked entity, Node s) { diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/PartialNeo4jEntityInstantiator.java b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/PartialNodeEntityInstantiator.java similarity index 89% rename from spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/PartialNeo4jEntityInstantiator.java rename to spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/PartialNodeEntityInstantiator.java index 726bc2a4d..7c2863cc2 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/PartialNeo4jEntityInstantiator.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/node/PartialNodeEntityInstantiator.java @@ -31,13 +31,13 @@ import javax.persistence.EntityManagerFactory; * @author Michael Hunger * @since 02.10.2010 */ -public class PartialNeo4jEntityInstantiator implements EntityInstantiator { +public class PartialNodeEntityInstantiator implements EntityInstantiator { - private final Neo4jConstructorGraphEntityInstantiator delegate; + private final NodeEntityInstantiator delegate; private EntityManagerFactory entityManagerFactory; - public PartialNeo4jEntityInstantiator(Neo4jConstructorGraphEntityInstantiator delegate, EntityManagerFactory entityManagerFactory) { + public PartialNodeEntityInstantiator(NodeEntityInstantiator delegate, EntityManagerFactory entityManagerFactory) { this.delegate = delegate; this.entityManagerFactory = entityManagerFactory; } diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/relationship/ConstructorBypassingGraphRelationshipInstantiator.java b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/relationship/RelationshipEntityInstantiator.java similarity index 92% rename from spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/relationship/ConstructorBypassingGraphRelationshipInstantiator.java rename to spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/relationship/RelationshipEntityInstantiator.java index f5d386c8c..53b1f5d86 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/relationship/ConstructorBypassingGraphRelationshipInstantiator.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/graph/neo4j/support/relationship/RelationshipEntityInstantiator.java @@ -34,7 +34,7 @@ import sun.reflect.ReflectionFactory; * Part of the SPI, not intended for public use. */ -public class ConstructorBypassingGraphRelationshipInstantiator extends AbstractConstructorEntityInstantiator { +public class RelationshipEntityInstantiator extends AbstractConstructorEntityInstantiator { @Override protected void setState(RelationshipBacked entity, Relationship relationship) { diff --git a/src/docbkx/reference/neo4j-intro.xml b/src/docbkx/reference/neo4j-intro.xml deleted file mode 100644 index 255de0dd9..000000000 --- a/src/docbkx/reference/neo4j-intro.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - Neo4j introduction - - ... - - diff --git a/src/docbkx/reference/programming-model/finders.xml b/src/docbkx/reference/programming-model/finders.xml deleted file mode 100644 index a3035ba98..000000000 --- a/src/docbkx/reference/programming-model/finders.xml +++ /dev/null @@ -1,76 +0,0 @@ - - -
- Finding nodes with finders - TODO: rewrite to repositories - Spring Data Graph also comes with a typed, repository-like Finder implementation that provides methods for - locating nodes and relationships. Those methods return instances of the node and relationship entities, - not the graph primitives from Neo4j. Finders delegate to the configured NodeTypeStrategy for type - based queries. - - - loading an instance via the Neo4j node id - T findById(id) - - - iterating over all nodes of a node entity type - Iterable<T>findAll() - - - counting the instances of a node entity type - long count() - - - iterating over all indexed instances with a certain property value - Iterable<T> findAllByPropertyValue(indexName, keyName, value) - - - getting a single instance with a certain property value - T findByPropertyValue(indexName, keyName, value) - - - iterating over all indexed instances within a certain numerical range (inclusive) - Iterable<T> findAllByRange(indexName, keyName, from, to) - - - iterating over all indexed instances matching the given fulltext (or QueryContext query) - Iterable<T> findAllByQuery(indexName, keyName, queryOrQueryContext) - - - iterating over a traversal result - Iterable<T> findAllByTraversal(startNode, traversalDescription) - - - The Finder instances are created via a FinderFactory to be bound to a concrete node or relationship entity class. - The FinderFactory is configured in the Spring context and can be injected. - graphRepository = graphRepositoryFactory.createNodeEntityFinder(Person.class); - -Person dave=graphRepository.findById(123); - -long numberOfPeople = graphRepository.count(); - -Person mark = graphRepository.findByPropertyValue(null,"name", "mark"); - -Iterable devs = graphRepository.findAllByProperyValue(null, "occupation","developer"); - -Iterable middleAgedPeople = graphRepository.findAllByRange(null, "age",20,40); - -Iterable aTeam = graphRepository.findAllByQuery(null, "name","A*"); - -Iterable davesFriends = graphRepository.findAllByTraversal(dave, - Traversal.description().pruneAfterDepth(1) - .relationships(KNOWS).filter(returnAllButStartNode())); -]]> - -
- NodeTypeStrategies: Storing Type Information in the Graph - - Internally the mapping from java types to the graph is handled by a NodeTypeStrategy instance - that is configured with the GraphDatabaseContext. The strategy is called on entity creation and - removal and provides methods for retrieving entities based on type. It also comes with methods confirming or - retrieving java types from the actual graph node. (see also - ) - -
-
\ No newline at end of file diff --git a/src/docbkx/reference/programming-model/repositories.xml b/src/docbkx/reference/programming-model/repositories.xml new file mode 100644 index 000000000..5c518ff80 --- /dev/null +++ b/src/docbkx/reference/programming-model/repositories.xml @@ -0,0 +1,168 @@ + + +
+ GraphRepositories for basic CRUD and find-operations + + The repositories provided by Spring Data Graph build on the composable repository infrastructure contained + in Spring Data Commons. + Those repositories allow the interface based composition of the final repository consisting of provided default + implementations for certain interfaces and additional custom implementations for other methods. + + + + Spring Data Graph provides only the infrastructure and some default repository implementations so far. In future + releases support for finders derived from method names, named queries and annotated query methods will be added. + (e.g. + findByName(name), + @Query(name = "find-by-name-query") findByName(name), + @Query(query = "{name:%s}") findByName(name)) + + + Spring Data Graph comes with typed repository implementations that provide methods for + locating node and relationship entities. There are 3 types of basic repository interfaces and implementations. + One CRUD-Repository (CRUDGraphRepository<T>) that provides basic operations, a IndexQueryExecutor + that delegates to Neo4j's internal indexing subsystem for executing queries. And last but not least + a TraversalQueryExecutor that handles Neo4J Traversals. + + + CRUDGraphRepository delegates to the configured TypeRepresentationStrategy + () + for type based queries. + + + loading an instance via the Neo4j node id + T findOne(id) + + + checks for existence via the Neo4j node id + boolean exists(id) + + + iterating over all nodes of a node entity type + Iterable<T> findAll() (supported in future versions: Iterable<T> findAll(Sort) and Page<T> findAll(Pageable)) + + + counting the instances of a node entity type + Long count() + + + saves the graph entities + T save(T) and Iterable<T> save(Iterable<T>) + + + deletes the graph entities + void delete(T), void; delete(Iterable<T>) and deleteAll() + + + + + + IndexQueryExecutor works with the indexing subsystem and provides methods to find entities by indexed properties, ranged queries of combination thereof. + + + iterating over all indexed instances with a certain property value + Iterable<T> findAllByPropertyValue(indexName, keyName, value) + + + getting a single instance with a certain property value + T findByPropertyValue(indexName, keyName, value) + + + iterating over all indexed instances within a certain numerical range (inclusive) + Iterable<T> findAllByRange(indexName, keyName, from, to) + + + iterating over all indexed instances matching the given fulltext (or QueryContext query) + Iterable<T> findAllByQuery(indexName, keyName, queryOrQueryContext) + + + + + + TraversalQueryExecutor works with the traversal framework. + + + iterating over a traversal result + Iterable<T> findAllByTraversal(startNode, traversalDescription) + + + + + The Repository instances are either created manually via a DirectGraphRepositoryFactory to be bound + o a concrete node or relationship entity class. + The DirectGraphRepositoryFactory is configured in the Spring context and can be injected. + + Using GraphRepositories + graphRepository = graphRepositoryFactory.createNodeEntityRepository(Person.class); + +Person michael = graphRepository.save(new Person("Michael",36)); + +Person dave=graphRepository.findOne(123); + +Long numberOfPeople = graphRepository.count(); + +Person mark = graphRepository.findByPropertyValue(null,"name", "mark"); + +Iterable devs = graphRepository.findAllByProperyValue(null, "occupation","developer"); + +Iterable middleAgedPeople = graphRepository.findAllByRange(null, "age",20,40); + +Iterable aTeam = graphRepository.findAllByQuery(null, "name","A*"); + +Iterable davesFriends = graphRepository.findAllByTraversal(dave, + Traversal.description().pruneAfterDepth(1) + .relationships(KNOWS).filter(returnAllButStartNode())); +]]> + + +
+ Composing Repositories + + The recommended way of providing repositories is to define a repository-interface per domain class and have the + mechanisms provided by the repository infrastructure automatically detect them and additional implementation + classes and create an injectable repository implementation to be used in services or other spring beans. + + Composing Repositories + , PersonRepositoryExtension { +} +// alternatively select some of the required repositories individually +public interface PersonRepository extends CRUDGraphRepository, IndexQueryExecutor, + TraversalQueryExecutor, PersonRepositoryExtension { +} +// provide a custom extension if needed +public interface PersonRepositoryExtension { + Iterable findFriends(Person person); +} +public class PersonRepositoryImpl implements PersonRepositoryExtension { + + // optionally inject default repository, or use DirectGraphRepositoryFactory + @Autowired PersonRepository baseRepository; + public Iterable findFriends(Person person) { + return baseRepository.findAllByTraversal(person, friendsTraversal); + } +} + +// configure the repositories, preferably via the datagraph:repositories namespace (graphDatabaseContext reference is optional) + + +// have it injected +@Autowired +PersonRepository personRepository; + + Person michael = personRepository.save(new Person("Michael",36)); + + Person dave=personRepository.findOne(123); + + Iterable devs = personRepository.findAllByProperyValue(null, "occupation","developer"); + + Iterable aTeam = graphRepository.findAllByQuery(null, "name","A*"); + + Iterable friends = personRepository.findFriends(dave); + ]]> + + + +
+
\ No newline at end of file