diff --git a/src/docbkx/index.xml b/src/docbkx/index.xml
index 3828ec44a..7dcf015a0 100644
--- a/src/docbkx/index.xml
+++ b/src/docbkx/index.xml
@@ -98,16 +98,16 @@
-
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
diff --git a/src/docbkx/reference/neo4j.xml b/src/docbkx/reference/neo4j.xml
index a8836d56e..52d9a8c37 100644
--- a/src/docbkx/reference/neo4j.xml
+++ b/src/docbkx/reference/neo4j.xml
@@ -1,29 +1,47 @@
- Introduction to the Neo4j Graph Database
+ Introduction to Neo4j
- Neo4j is a graph database, a fully transactional database that stores data structured as graphs. A graph is a flexible data structure that allows for a more agile and rapid style of development.
-
+ Neo4j is a graph database. It is a fully transactional database that
+ stores data structured as graphs. A graph consists of nodes, connected by relationships. It is a flexible
+ data structure that allows for high query performance on complex data, while being intuitive for the
+ developer.
+
- Neo4j has been in commercial development for 10 years and in production for over 7 years. It is a mature and robust graph database that provides:
-
- an intuitive graph-oriented model for data representation. Instead of static and rigid tables, rows and columns, you work with a flexible graph network consisting of nodes, relationships and properties.
- a disk-based, native storage manager completely optimized for storing graph structures for maximum performance and scalability.
- massive scalability. Neo4j can handle graphs of several billion nodes/relationships/properties on a single machine and can be sharded to scale out across multiple machines.
- a powerful traversal framework for high-speed traversals in the node space.
- can be deployed as a full server or a very slim database with a small
- footprint (~500k jar).
- a simple and convenient object-oriented API.
-
-
+ Neo4j has been in commercial development for 10 years and in production for over 7 years. It is a mature and
+ robust graph database that:
+
+ has an intuitive graph-oriented model for data representation. Instead of tables, rows, and columns,
+ you work with a flexible graph network consisting of
+ nodes, relationships, and properties.
+
+ has a disk-based, native storage manager completely optimized for storing graph structures for maximum
+ performance and scalability.
+
+ is scalable. Neo4j can handle graphs of several billion nodes/relationships/properties on
+ a single machine, but can also be scaled out across multiple machines for high availability.
+
+ has a powerful traversal framework for fast traversals in the node space.
+
+ can be deployed as a standalone server or an embedded database with a very small footprint
+ (~700k jar).
+
+ has a simple and convenient API.
+
+
+
- In addition, Neo4j includes the usual database features: ACID transactions, durable persistence, concurrency control, transaction recovery, high availability and everything else you’d expect from an enterprise-strength database. Neo4j is released under a dual free software/commercial license model.
-
- What is a graph database?
- A graph database is a storage engine that is specialized in storing and retrieving vast networks of data. It efficiently stores nodes and relationship and allows high performance traversal of those structures. With property graphs it is possible to add an arbitrary number of properties to nodes
- and relationships which can be used directly or during traversals.
-
+ In addition, Neo4j includes the usual database features: ACID transactions, durable persistence,
+ concurrency control, transaction recovery, high availability and everything else you’d expect from an
+ enterprise database. Neo4j is released under a dual free software/commercial license model.
+
+ What is a graph database?
+ A graph database is a storage engine that is specialized in storing and retrieving vast networks of
+ data. It efficiently stores nodes and relationship and allows high performance traversal of those
+ structures. With property graphs it is possible to add an arbitrary number of properties to nodes
+ and relationships.
+
GraphDatabaseService
The interface org.neo4j.graphdb.GraphDatabaseService provides access to the storage engine. Its features include creating and retrieving Nodes and Relationships, managing indexes, via an IndexManager, database lifecycle callbacks, transation management and more.
diff --git a/src/docbkx/reference/programming-model.xml b/src/docbkx/reference/programming-model.xml
deleted file mode 100644
index d3f5e9eb5..000000000
--- a/src/docbkx/reference/programming-model.xml
+++ /dev/null
@@ -1,559 +0,0 @@
-
-
-
- Programming model for Spring Data Graph
- This chapter covers the fundamentals of the programming model behind Spring Data Graph. It discusses the AspectJ features used and the annotations
- provided by Spring Data Graph and how to use them.
- Examples for this section are taken from the imdb project of Spring Data Graph examples.
-
-
- Overview of the AspectJ support
- Behind the scenes Spring Data Graph leverages AspectJ aspects to modify the behavior of simple POJO entities to be
- able to be backed by a graph store. Each entity is backed by a node that holds its properties and
- relationships to other entities. AspectJ is used to intercept field access and to reroute it to the backing
- state (either its properties or relationships). For relationship entities the fields are similarly mapped to
- properties. There are two specially annotated fields for the start and the end node of the relationship.
-
-
- The aspect introduces some internal fields and some public methods to the entities for accessing the backing state via getPersistentState() and creating relationships with relateTo and retrieving relationship entities via getRelationshipTo. It also introduces finder methods like find(Class<? extends NodeEntity>, TraversalDescription) and equals and hashCode delegation.
-
-
- Spring Data Graph internally uses an abstraction called EntityState that the field access and instantiation advices of the aspect delegate to, keeping the aspect code very small and focused to the pointcuts and delegation code. The EntityState then uses a number of FieldAccessor factories to create a FieldAccessor instance per field that does the specific handling needed for the concrete field.
-
-
-
- Using annotations to define POJO entities and relationships
- Entities are declared using the @NodeEntity annotation. Relationship entities use the @RelationshipEntity annotation.
-
- Entities with @NodeEntity
- The @NodeEntity annotation is used to declare a POJO entity to be backed by a node in the graph store. Simple fields on the entity
- are mapped by default to properties of the node. Object references to other NodeEntities (whether single
- or Collection) are mapped via relationships. If the annotation parameter useShortNames
- is set to false, the properties and relationship names used will be prepended with the class name of the
- entity. If the parameter fullIndex is set to true, all fields of the entity will be indexed. If the
- partial parameter is set to true, this entity takes part in a cross-store setting where only
- the parts of the entity not handled by JPA will be mapped to the graph store.
-
- Entity fields can be annotated with @GraphProperty, @RelatedTo, @RelatedToVia, @Indexed and @GraphId
-
-
-
- RelationshipEntities with @RelationshipEntity
- To access the rich data model of graph relationships, POJOs can also be annotated with
- @RelationshipEntity. Relationship entities can't be instantiated directly but are rather accessed via
- node entities, either by @RelatedToVia fields or by the relateTo
- or getRelationshipTo methods.
- Relationship entities may contain fields that are mapped to properties and two special fields that are
- annotated with @StartNode and @EndNode which point to the start and end node entities respectively. These fields are treated as read only fields.
-
-
-
-
- Fields with @GraphProperty
- It is not necessary to annotate fields as they are persisted by default; all fields that contain primitive values are persisted directly to the graph. All fields
- convertible to String using the Spring conversion services will be stored as a string. Transient fields are not persisted.
- This annotation is mainly used for cross-store persistence.
-
-
- Fields with @RelatedTo pointing to other NodeEntities
-
- Relationships to other NodeEntities are mapped to graph relationships. Those can either be single
- relationships (1:1) or multiple relationships (1:n). In most cases single relationships to other
- node entities don't have to be annotated as Spring Data Graph can extract all necessary information from the field
- using reflection. In the case of
- multiple relationships, the elementClass parameter of @RelatedTo must be specified because of type erasure.
- The direction (default OUTGOING) and type
- (inferred from field name) parameters of the annotation are optional.
-
- Relationships to single node entities are created when setting the field and deleted when setting it to null. For multi-relationships the field provides a managed collection (Set) that handles addition and removal of node entities and reflects those in the graph relationships.
- movies;
-}
-]]>
-
-
- Fields with @RelatedToVia pointing to RelationshipEntities
- To provide easy programmatic access to the richer relationship entities of the data model a different
- annotation @RelatedToVia can be declared on fields of Iterables of the relationship entity type. These
- Iterables then provide read only access to instances of the entity that backs the relationship of this
- relationship type. Those instances are initialized with the properties of the relationship and the start
- and end node.
-
- roles;
-}
-]]>
-
-
- @StartNode
- Annotation for the start node of a relationship entity, read only.
-
-
- @EndNode
- Annotation for the end node of a relationship entity, read only.
-
-
- @Indexed
- The @Indexed annotation can be declared on fields that are intended to be indexed by the Neo4j
- IndexManager,
- triggered by value modification.
- The resulting index can be used to later retrieve nodes or relationships that contain a certain property
- value (for example a name). Often an index is used to establish the start node for a traversal.
- Indexes are accessed by a Finder for a particular NodeEntity or RelationshipEntity, created via a FinderFactory.
-
-
- GraphDatabaseContext exposes the
- indexes for Nodes and Relationships. Indexes can
- be named, for instance to keep separate domain concepts in separate indexes. That's why it is possible
- to specifiy an index name with
- the @Indexed annotation. It can also be specified at the entity level, this name is then the default
- index name for
- all fields of the entity. If no index name is specified, it defaults to the one configured with Neo4j
- ("node" and "relationship").
-
-
-
- @GraphTraversal
- The @GraphTraversal annotation leverages the delegation infrastructure used by the Spring Data Graph aspects.
- It provides dynamic fields
- which, when accessed, return an Iterable of NodeEntities that are the result of a traversal starting at the
- current NodeEntity.
- The TraversalDescription used for this is created by a TraversalDescriptionBuilder whose class is
- referred to by the
- traversalBuilder
- attribute of the annotation. The class of the expected NodeEntities is provided with the
- elementClass
- attribute.
-
-
-
-
- Finding Nodes with Finders
- Spring Data Graph also comes with a type bound Repository-like
- Finder implementation that provides methods for locating nodes
- and relationships:
-
- using direct access findById(id) ,
- iterating over all nodes of a node entity type (findAll),
- counting the instances of a node entity type (count),
- iterating over all indexed instances with a certain property value (findAllByPropertyValue),
- getting a single instance with a certain property value (findByPropertyValue),
- iterating over all indexed instances within a certain numerical range (inclusive) (findAllByRange),
- iterating over a traversal result (findAllByTraversal).
-
- The Finder instances are created via a FinderFactory to be bound to a
- concrete node or relationship entity class.
- The FinderFactory is created in the Spring context and can be
- injected.
- finder = finderFactory.createNodeEntityFinder(Person.class);
-Person dave=finder.findById(123);
-int people = finder.count();
-Person mark = finder.findByPropertyValue("name", "mark");
-Iterable devs = finder.findAllByProperyValue("occupation","developer");
-Iterable davesFriends = finder.findAllByTraversal(dave,
- Traversal.description().pruneAfterDepth(1)
- .relationships(KNOWS).filter(returnAllButStartNode()));
-
-]]>
-
-
-
- Representing Java Types via NodeTypeStrategy
-
- There are several ways to represent the Java type hierarchy of the data model in the graph. In general for all node and relationship
- entities type information is needed to perform certain repository operations. That's why the hierarchy up to java.lang.Object of all
- these classes will be persisted in the graph. Implementations of NodeTypeStrategy take care of persisting this information on entity instance
- creation. They also provide the repository methods that use this type information to perform their operations like findAll, count etc.
-
-
- The current implementation uses nodes to represent the Java type hierarchy which are connected via SUBCLASS_OF relationships to their superclass
- nodes and via INSTANCE_OF relationships to the concrete node entity instance node.
-
-
- An alternative approach could use indexing operations to perform the same functionality. Or one could skip the NodeTypeStrategy altogether if no
- strict checks on type conformity are needed, which would allow for a much more flexible data model.
-
-
-
- Methods added to Entity Classes
-
- The node and relationship aspects introduce (via ITD - inter type declaration) several methods to the entities that
- make common tasks easier. Unfortunately these methods are not generified yet, so the
- results have to be casted to the correct return type.
-
-
- accessing node and relationship ids
-
- nodeEntity.getNodeId() and relationshipEntity.getRelationshipId()
-
-
-
- accessing the node or relationship backing the entity
-
- entity.getPersistentState()
-
-
-
- equals and hashcode are delegated to the underlying state
-
- entity.equals() and entity.hashCode()
-
-
-
- creating relationships to a target node entity
-
- nodeEntity.relateTo(targetEntity, relationshipClass, relationshipType)
-
-
-
- retrieving a single relationship
-
- nodeEntity.getRelationshipTo(targetEnttiy, relationshipClass, relationshipType)
-
-
-
- removing a single relationship
-
- nodeEntity.removeRelationshipTo(targetEntity, relationshipType)
-
-
-
- remove the node entity, its relationship and index entries
-
- entity.remove()
-
-
-
- projecting to a different target type
-
- entity.projectTo(targetClass)
-
-
-
- traversing, starting at the current node
-
- nodeEntity.findAllByTraversal(targetType, traversalDescription)
-
-
-
-
-
-
- Dynamic Typing - Projection to unrelated, fitting types
-
- As the underlying data model of a graph database doesn't imply and enforce strict type constraints like a relational
- model does, it offers much more flexibility on how to model your domain classes and which of those to use in different
- contexts.
-
-
- For instance an order can be used in these contexts: customer, procurement, logistics, billing, fulfillment and many more.
- Each of those contexts requires its distinct set of attributes and operations. As Java doesn't support mixins one would put
- the sum of all of those into the entity class and thereby making it very big, brittle and hard to understand.
- Being able to take a basic order and project it to a different (not related in the inheritance hierarchy or even an interface)
- order type that is valid in the current context and only offers the attributes and methods needed here would be very benefitial.
-
- Spring Data Graph offers initial support for projecting node and relationship entities to different target types.
- All instances of this projected entity share the same backing node or relationship, so data changes are reflected
- immediately.
-
-
- This could for instance also be used to handle nodes of a traversal with a unified (simpler) type (e.g. for
- reporting or auditing) and only project them to a concrete, more functional target type when the business
- logic requires it.
-
- trainings;
-}
-
-for (Person person : finder.findAllByProperyValue("occupation","developer")) {
- Developer developer=person.projectTo(Developer.class)
- if (developer.isJavaDeveloper()) {
- trainInSpringData(developer.projectTo(Trainee.class));
- }
-}
-]]>
-
-
-
- Neo4jTemplate
- The Neo4jTemplate offers the convenient API of Spring templates for the Neo4j graph database.
-
- There are methods for creating nodes and relationships that automatically set provided properties and optionally
- index certain fields. Other methods ( index , autoindex) will index them.
-
- For the querying operations Neo4jTemplate unifies the result with the Path abstraction that comes from Neo4j.
- Much like a resultset a path contains nodes() and relationships()
- starting at a startNode() and
- ending with a endNode(), the lastRelationship() is also available separately.
- The Path abstraction also wraps results that contain just nodes or relationships.
- Using implementations of PathMapper<T> and PathMapper.WithoutResult (comparable with RowMapper and
- RowCallbackHandler) the paths can be converted to Java objects.
-
- Query methods either take a field / value combination to look for exact matches in the index or a lucene query
- object or string to handle more complex queries.
-
- Traversal methods are the bread and butter of graph operations. As such, they are fully supported in the Neo4jTemplate.
- The traverseNext method traverses to the direct neighbours of the start node filtering the relationships according
- to its parameters.
-
- The traverse method covers the full fledged traversal operation that takes a powerful TraversalDescription
- (most probably built from the Traversal.description() DSL) and runs it from the start node. Each path that is returned
- via the traversal is passed to the PathMapper to be processed accordingly.
-
- The Neo4jTemplate provides configurable implicit transactions for all its methods. By default it creates a transaction
- for each call (which is a no-op if there is already a transaction running). If you call the constructor
- with the useExplicitTransactions parameter set to true, it won't create any transactions so you have to
- provide them using @Transactional or the TransactionTemplate.
-
-
-
-
-
-
- Indexing
-
- The Neo4j graph database can use different index providers for exact lookups and fulltext searches. Lucene is used as a index provider implementation. There is support for distinct indexes for nodes and relationships
- which can be configured to be of fulltext or exact types.
-
-
- Using the standard Neo4j API, Nodes and Relationships and their indexed field-value combinations
- have to be added manually to the appropriate index. When using Spring Data Graph, this task is simplified by eased by applying an @Indexed annotation on entity fields. This will result in updates to the index on
- every change. Numerical fields are indexed numerically so that they are available for range queries. All other
- fields are indexed with their string representation. The @Indexed annotation can also set the index-name to be used.
- If @Indexed annotates the entity class, the index-name for the whole entity is preset to that value. Not providing
- index names defaults them to "node" and "relationship" respectively.
-
-
- Query access to the index happens with the Node- and RelationshipFinders that are created via an instance of org.springframework.data.graph.neo4j.finder.FinderFactory.
-
- The methods findByPropertyValue and findAllByPropertyValue work on the exact indexes and return the first or all
- matches. To do range queries, use findAllByRange (please note that currently both values are inclusive).
-
- people;
-}
-
-NodeFinder finder = finderFactory.createNodeEntityFinder(Person.class);
-
-// exact finder
-Person mark = finder.findByProperyValue("people","name","mark");
-
-// numeric range queries
-for (Person middleAgedDeveloper : finder.findAllByRange(null, "age", 20, 40)) {
- Developer developer=middleAgedDeveloper.projectTo(Developer.class);
-}
-]]>
-
- Neo4jTemplate also offers index support, providing auto-indexing for fields at creation time of nodes and relationships.
- There is an autoIndex method that can also add indexes for a set of fields in one go.
-
-
- For querying the index, the template offers query-methods that take either the exact match parameters or a query object /
- query expression and push the results wrapped uniformly as Paths to the supplied PathMapper to be converted or collected.
-
-
-
- Transactions in Spring Data Graph
-
- Neo4j is a transactional datastore which only allows modifications within transaction boundaries and fullfills the ACID properties.
- Reading from the store is also possible outside of transactions.
-
-
- Spring Data Graph integrates with transaction managers configured using Spring. The simplest scenario of
- just running the graph database uses a SpringTransactionManager provided by the Neo4j kernel to be used
- with Spring's JtaTransactionManager.
-
-Note: The explicit XML configuration given below is encoded in the Neo4jConfiguration configuration bean that uses Spring's @Configuration functioanlity. This simplifies the configuration. An example is shown further below.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ]]>
-
- For scenarios running multiple transactional resources there are two options.
- First of all you can have Neo4j participate in the externally set up transaction manager using the new
- SpringProvider by enabling the configuration parameter for your graph database. Either via the spring config
- or the configuration file (neo4j.properties).
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ]]>
-
- You can configure a stock XA transaction manager to be used with Neo4j and the other resources (e.g. Atomikos, JOTM,
- App-Server-TM). For a bit less secure but fast 1 phase commit best effort, use the implementation coming
- with Spring Data Graph (ChainedTransactionManager).
- It takes a list of transaction-managers as constructor params and will handle them in order for transaction
- start and commit (or rollback) in the reverse order.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ]]>
-
-
- Bean Validation - JSR-303
- Spring Data Graph supports property based validation support. So whenever a property is changed, it is
- checked against the annotated constraints (.e.g @Min, @Max, @Size, etc).
- Validation errors throw a ValidationException. For evaluating the constraints the validation support that comes
- with Spring is used. To use it a validator has to be registered with the GraphDatabaseContext, if there is none,
- no validation will be performed (any registered Validator or (Local)ValidatorFactoryBean will be used).
-
-
-
-
-
- Session handling - attached and detached Entities
-
- By default newly created node entities are in a detached state. When persist() is called on the entity
- it is attached to the graph store and its properties and relationships are persisted as well. Changing an attached
- entity inside a transaction will write through the changes to the datastore. Whenever an entity is changed outside
- of a transaction it will be considered detached. The changed data is stored in the entity itself and not written
- back to the datastore.
-
-
- All entities that are returned by library functions are initially in an attached state. Changing them outside of a
- transaction detaches them. For writing the changes back it is necessary to persist() them again.
-
-
- Persisting an entity not only persists that single entity but will traverse its existing and new relationships and
- persist the cluster of detached entities that it is part of. The borders of this cluster are formed by attached entities.
- The persist operation creates its own, implicit transaction. When it is called withina external transaction it participates
- otherwise it is an atomic operation.
-
-
- Please keep in mind that the session handling behaviour is still heavily developed. The defaults and also other
- aspects of the behaviour are likely to change in subsequent releases. At the moment there is no support for the creation
- of relationships outside of transactions and also more complex operations like creating whole subgraphs is not supported.
-
-
-
-
-
diff --git a/src/docbkx/reference/programming-model/annotations.xml b/src/docbkx/reference/programming-model/annotations.xml
new file mode 100644
index 000000000..4f42f682c
--- /dev/null
+++ b/src/docbkx/reference/programming-model/annotations.xml
@@ -0,0 +1,141 @@
+
+
+
+ Using annotations to define POJO entities and relationships
+ Entities are declared using the @NodeEntity annotation. Relationship entities use the
+ @RelationshipEntity
+ annotation.
+
+
+ Entities with @NodeEntity
+ The @NodeEntity annotation is used to declare a POJO entity to be backed by a node in the
+ graph store. Simple fields on the entity are mapped by default to properties of the node. Object
+ references to other NodeEntities (whether single or Collection) are mapped via relationships. If
+ the annotation parameter useShortNames is set to false, the properties and relationship
+ names used will be prepended with the class name of the entity. If the parameter fullIndex
+ is set to true, all fields of the entity will be indexed. If the partial
+ parameter is set to true, this entity takes part in a cross-store setting where only
+ the parts of the entity not handled by JPA will be mapped to the graph store.
+
+ Entity fields can be annotated with @GraphProperty, @RelatedTo, @RelatedToVia, @Indexed and @GraphId
+
+
+
+
+ RelationshipEntities with @RelationshipEntity
+ To access the rich data model of graph relationships, POJOs can also be annotated with
+ @RelationshipEntity. Relationship entities can't be instantiated directly but are rather accessed via
+ node entities, either by @RelatedToVia fields or by the relateTo or
+ getRelationshipTo methods.
+ Relationship entities may contain fields that are mapped to properties and two special fields that are
+ annotated with @StartNode and @EndNode which point to the start and end node entities respectively. These
+ fields are treated as read only fields.
+
+
+
+
+ Fields with @GraphProperty
+ It is not necessary to annotate fields as they are persisted by default; all fields that contain primitive
+ values are persisted directly to the graph. All fields
+ convertible to String using the Spring conversion services will be stored as a string. Transient fields are
+ not persisted.
+ This annotation is mainly used for cross-store persistence.
+
+
+
+ Fields with @RelatedTo pointing to other NodeEntities
+
+ Relationships to other NodeEntities are mapped to graph relationships. Those can either be single
+ relationships (1:1) or multiple relationships (1:n). In most cases single relationships to other
+ node entities don't have to be annotated as Spring Data Graph can extract all necessary information
+ from the field using reflection. In the case of multiple relationships, the elementClass
+ parameter of @RelatedTo must be specified because of type erasure. The direction
+ (default OUTGOING) and type (inferred from field name) parameters of the annotation are
+ optional.
+
+ Relationships to single node entities are created when setting the field and deleted when setting it to
+ null. For multi-relationships the field provides a managed collection (Set) that handles addition and
+ removal of node entities and reflects those in the graph relationships.
+
+ movies;
+}
+]]>
+
+
+ Fields with @RelatedToVia pointing to RelationshipEntities
+ To provide easy programmatic access to the richer relationship entities of the data model a different
+ annotation @RelatedToVia can be declared on fields of Iterables of the relationship entity type. These
+ Iterables then provide read only access to instances of the entity that backs the relationship of this
+ relationship type. Those instances are initialized with the properties of the relationship and the start
+ and end node.
+
+ roles;
+}
+]]>
+
+
+ @StartNode
+ Annotation for the start node of a relationship entity, read only.
+
+
+ @EndNode
+ Annotation for the end node of a relationship entity, read only.
+
+
+ @Indexed
+ The @Indexed annotation can be declared on fields that are intended to be indexed by the Neo4j
+ IndexManager, triggered by value modification.
+ The resulting index can be used to later retrieve nodes or relationships that contain a certain property
+ value (for example a name). Often an index is used to establish the start node for a traversal.
+ Indexes are accessed by a Finder for a particular NodeEntity or RelationshipEntity, created via a
+ FinderFactory.
+
+
+ GraphDatabaseContext exposes the indexes for Nodes and Relationships. Indexes can
+ be named, for instance to keep separate domain concepts in separate indexes. That's why it is possible
+ to specifiy an index name with the @Indexed annotation. It can also be specified at the entity level,
+ this name is then the default index name for all fields of the entity. If no index name is specified,
+ it defaults to the one configured with Neo4j ("node" and "relationship").
+
+
+
+ @GraphTraversal
+ The @GraphTraversal annotation leverages the delegation infrastructure used by the Spring Data Graph
+ aspects. It provides dynamic fields which, when accessed, return an Iterable of NodeEntities that are
+ the result of a traversal starting at the current NodeEntity. The TraversalDescription used for this
+ is created by a TraversalDescriptionBuilder whose class is referred to by the traversalBuilder
+ attribute of the annotation. The class of the expected NodeEntities is provided with the
+ elementClass attribute.
+
+
+
\ No newline at end of file
diff --git a/src/docbkx/reference/programming-model/aspectj.xml b/src/docbkx/reference/programming-model/aspectj.xml
new file mode 100644
index 000000000..461f2f2f8
--- /dev/null
+++ b/src/docbkx/reference/programming-model/aspectj.xml
@@ -0,0 +1,25 @@
+
+
+
+ Overview of the AspectJ support
+ Behind the scenes Spring Data Graph leverages AspectJ aspects to modify the behavior of simple POJO entities
+ to be
+ able to be backed by a graph store. Each entity is backed by a node that holds its properties and
+ relationships to other entities. AspectJ is used to intercept field access and to reroute it to the backing
+ state (either its properties or relationships). For relationship entities the fields are similarly mapped to
+ properties. There are two specially annotated fields for the start and the end node of the relationship.
+
+
+ The aspect introduces some internal fields and some public methods to the entities for accessing the backing
+ state via getPersistentState() and creating relationships with relateTo
+ and retrieving relationship entities viagetRelationshipTo. It also introduces finder methods like
+ find(Class<? extends NodeEntity>, TraversalDescription)
+ and equals and hashCode delegation.
+
+
+ Spring Data Graph internally uses an abstraction called EntityState that the field access and instantiation
+ advices of the aspect delegate to, keeping the aspect code very small and focused to the pointcuts and
+ delegation code. The EntityState then uses a number of FieldAccessor factories to create a FieldAccessor
+ instance per field that does the specific handling needed for the concrete field.
+
+
\ No newline at end of file
diff --git a/src/docbkx/reference/programming-model/attachdetach.xml b/src/docbkx/reference/programming-model/attachdetach.xml
new file mode 100644
index 000000000..439c0d828
--- /dev/null
+++ b/src/docbkx/reference/programming-model/attachdetach.xml
@@ -0,0 +1,36 @@
+
+
+
+ Session handling - attached and detached entities
+
+ By default newly created node entities are in a detached state. When persist() is called on the
+ entity it is attached to the graph store and its properties and relationships are persisted as well. Changing
+ an attached entity inside a transaction will write through the changes to the datastore. Whenever an entity
+ is changed outside of a transaction it will be considered detached. The changed data is stored in the entity
+ itself and not written back to the datastore.
+
+
+ All entities that are returned by library functions are initially in an attached state. Changing them outside
+ of a transaction detaches them. For writing the changes back it is necessary to persist() them
+ again.
+
+
+ Persisting an entity not only persists that single entity but will traverse its existing and new relationships
+ and persist the cluster of detached entities that it is part of. The borders of this cluster are formed by
+ attached entities. The persist operation creates its own, implicit transaction. When it is called withina
+ external transaction it participates otherwise it is an atomic operation.
+
+
+ Please keep in mind that the session handling behaviour is still heavily developed. The defaults and also
+ other aspects of the behaviour are likely to change in subsequent releases. At the moment there is no support
+ for the creation of relationships outside of transactions and also more complex operations like creating
+ whole subgraphs outside of transactions is not supported.
+
+
+
diff --git a/src/docbkx/reference/programming-model/beanvalidation.xml b/src/docbkx/reference/programming-model/beanvalidation.xml
new file mode 100644
index 000000000..da2bdfaea
--- /dev/null
+++ b/src/docbkx/reference/programming-model/beanvalidation.xml
@@ -0,0 +1,24 @@
+
+
+
+ Bean Validation - JSR-303
+
+ Spring Data Graph supports property based validation support. So whenever a property is changed, it is
+ checked against the annotated constraints (.e.g @Min, @Max, @Size, etc).
+ Validation errors throw a ValidationException. For evaluating the constraints the validation support that
+ comes with Spring is used. To use it a validator has to be registered with the GraphDatabaseContext, if there
+ is none, no validation will be performed (any registered Validator or (Local)ValidatorFactoryBean will be
+ used).
+
+
+
diff --git a/src/docbkx/reference/programming-model/finders.xml b/src/docbkx/reference/programming-model/finders.xml
new file mode 100644
index 000000000..f547f4760
--- /dev/null
+++ b/src/docbkx/reference/programming-model/finders.xml
@@ -0,0 +1,50 @@
+
+
+
+ Finding nodes with finders
+ Spring Data Graph also comes with a type bound Repository-like
+ Finder implementation that provides methods for locating nodes
+ and relationships:
+
+
+ using direct access findById(id),
+
+
+ iterating over all nodes of a node entity type (findAll),
+
+
+ counting the instances of a node entity type (count),
+
+
+ iterating over all indexed instances with a certain property value (findAllByPropertyValue),
+
+
+
+ getting a single instance with a certain property value (findByPropertyValue),
+
+
+ iterating over all indexed instances within a certain numerical range (inclusive)
+ (findAllByRange),
+
+
+
+ iterating over a traversal result (findAllByTraversal).
+
+
+ The Finder instances are created via a FinderFactory to be bound to a
+ concrete node or relationship entity class.
+ The FinderFactory is created in the Spring context and can be
+ injected.
+ finder = finderFactory.createNodeEntityFinder(Person.class);
+Person dave=finder.findById(123);
+int people = finder.count();
+Person mark = finder.findByPropertyValue("name", "mark");
+Iterable devs = finder.findAllByProperyValue("occupation","developer");
+Iterable davesFriends = finder.findAllByTraversal(dave,
+ Traversal.description().pruneAfterDepth(1)
+ .relationships(KNOWS).filter(returnAllButStartNode()));
+
+]]>
+
+
\ No newline at end of file
diff --git a/src/docbkx/reference/programming-model/indexing.xml b/src/docbkx/reference/programming-model/indexing.xml
new file mode 100644
index 000000000..826c02288
--- /dev/null
+++ b/src/docbkx/reference/programming-model/indexing.xml
@@ -0,0 +1,68 @@
+
+
+
+ Indexing
+
+ The Neo4j graph database can use different index providers for exact lookups and fulltext searches. Lucene is
+ used as a index provider implementation. There is support for distinct indexes for nodes and relationships
+ which can be configured to be of fulltext or exact types.
+
+
+ Using the standard Neo4j API, Nodes and Relationships and their indexed field-value combinations
+ have to be added manually to the appropriate index. When using Spring Data Graph, this task is simplified by
+ eased by applying an @Indexed annotation on entity fields. This will result in updates to the
+ index on every change. Numerical fields are indexed numerically so that they are available for range queries.
+ All other fields are indexed with their string representation. The @Indexed annotation can also set the
+ index-name to be used. If @Indexed annotates the entity class, the index-name for the whole entity is preset
+ to that value. Not providing index names defaults them to "node" and "relationship" respectively.
+
+
+ Query access to the index happens with the Node- and RelationshipFinders that are created via an instance of
+ org.springframework.data.graph.neo4j.finder.FinderFactory. The methods
+ findByPropertyValue and findAllByPropertyValue work on the exact indexes and
+ return the first or all matches. To do range queries, use findAllByRange (please note that
+ currently both values are inclusive).
+
+ people;
+}
+
+NodeFinder finder = finderFactory.createNodeEntityFinder(Person.class);
+
+// exact finder
+Person mark = finder.findByProperyValue("people","name","mark");
+
+// numeric range queries
+for (Person middleAgedDeveloper : finder.findAllByRange(null, "age", 20, 40)) {
+ Developer developer=middleAgedDeveloper.projectTo(Developer.class);
+}
+]]>
+
+ Neo4jTemplate also offers index support, providing auto-indexing for fields at creation time of nodes and
+ relationships. There is an autoIndex method that can also add indexes for a set of fields in one
+ go.
+
+
+ For querying the index, the template offers query-methods that take either the exact match parameters or a query
+ object / query expression and push the results wrapped uniformly as Paths to the supplied
+ PathMapper to be converted or collected.
+
+
diff --git a/src/docbkx/reference/programming-model/introducedmethods.xml b/src/docbkx/reference/programming-model/introducedmethods.xml
new file mode 100644
index 000000000..a62d8c290
--- /dev/null
+++ b/src/docbkx/reference/programming-model/introducedmethods.xml
@@ -0,0 +1,66 @@
+
+
+
+ Methods added to entity classes
+
+ The node and relationship aspects introduce (via ITD - inter type declaration) several methods to the
+ entities that make common tasks easier. Unfortunately these methods are not generified yet, so the
+ results have to be casted to the correct return type.
+
+
+ accessing node and relationship ids
+
+ nodeEntity.getNodeId() and relationshipEntity.getRelationshipId()
+
+
+
+ accessing the node or relationship backing the entity
+
+ entity.getPersistentState()
+
+
+
+ equals and hashcode are delegated to the underlying state
+
+ entity.equals() and entity.hashCode()
+
+
+
+ creating relationships to a target node entity
+
+ nodeEntity.relateTo(targetEntity, relationshipClass, relationshipType)
+
+
+
+ retrieving a single relationship
+
+ nodeEntity.getRelationshipTo(targetEnttiy, relationshipClass, relationshipType)
+
+
+
+ removing a single relationship
+
+ nodeEntity.removeRelationshipTo(targetEntity, relationshipType)
+
+
+
+ remove the node entity, its relationship and index entries
+
+ entity.remove()
+
+
+
+ projecting to a different target type
+
+ entity.projectTo(targetClass)
+
+
+
+ traversing, starting at the current node
+
+ nodeEntity.findAllByTraversal(targetType, traversalDescription)
+
+
+
+
+
diff --git a/src/docbkx/reference/programming-model/neo4jtemplate.xml b/src/docbkx/reference/programming-model/neo4jtemplate.xml
new file mode 100644
index 000000000..7dde28a54
--- /dev/null
+++ b/src/docbkx/reference/programming-model/neo4jtemplate.xml
@@ -0,0 +1,51 @@
+
+
+
+ Neo4jTemplate
+
+ The Neo4jTemplate offers the convenient API of Spring templates for the Neo4j graph database.
+ There are methods for creating nodes and relationships that automatically set provided properties and optionally
+ index certain fields. Other methods (index, autoindex) will index them.
+
+
+ For the querying operations Neo4jTemplate unifies the result with the Path abstraction that
+ comes from Neo4j. Much like a resultset a path contains nodes() and relationships()
+ starting at a startNode() and ending with aendNode(), the
+ lastRelationship() is also available separately. The Path abstraction also wraps
+ results that contain just nodes or relationships. Using implementations of PathMapper<T>
+ and PathMapper.WithoutResult (comparable with RowMapper and
+ RowCallbackHandler) the paths can be converted to Java objects.
+
+
+ Query methods either take a field / value combination to look for exact matches in the index or a lucene query
+ object or string to handle more complex queries.
+
+
+ Traversal methods are the bread and butter of graph operations. As such, they are fully supported in the
+ Neo4jTemplate. The traverseNext method traverses to the direct neighbours of the
+ start node filtering the relationships according to its parameters.
+
+
+ The traverse method covers the full fledged traversal operation that takes a powerful
+ TraversalDescription (most probably built from the Traversal.description()
+ DSL) and runs it from the start node. Each path that is returned via the traversal is passed to the
+ PathMapper to be processed accordingly.
+
+
+ The Neo4jTemplate provides configurable implicit transactions for all its methods. By default
+ it creates a transaction for each call (which is a no-op if there is already a transaction running). If
+ you call the constructor with the useExplicitTransactions parameter set to true, it won't
+ create any transactions so you have to provide them using @Transactional or the TransactionTemplate.
+
+
+
+
diff --git a/src/docbkx/reference/programming-model/nodetypestrategy.xml b/src/docbkx/reference/programming-model/nodetypestrategy.xml
new file mode 100644
index 000000000..fb93dd6d6
--- /dev/null
+++ b/src/docbkx/reference/programming-model/nodetypestrategy.xml
@@ -0,0 +1,23 @@
+
+
+
+ Reified types for entities
+
+ There are several ways to represent the Java type hierarchy of the data model in the graph. In general for all
+ node and relationship entities type information is needed to perform certain repository operations. That's
+ why the hierarchy up to java.lang.Object of all these classes will be persisted in the graph.
+ Implementations of NodeTypeStrategy take care of persisting this information on entity instance
+ creation. They also provide the repository methods that use this type information to perform their operations
+ like findAll, count etc.
+
+
+ The current implementation uses nodes to represent the Java type hierarchy which are connected via SUBCLASS_OF
+ relationships to their superclass nodes and via INSTANCE_OF relationships to the concrete node entity
+ instance node.
+
+
+ An alternative approach could use indexing operations to perform the same functionality. Or one could skip the
+ NodeTypeStrategy altogether if no strict checks on type conformity are needed, which would allow for a much
+ more flexible data model.
+
+
\ No newline at end of file
diff --git a/src/docbkx/reference/programming-model/programming-model.xml b/src/docbkx/reference/programming-model/programming-model.xml
new file mode 100644
index 000000000..4cea04b08
--- /dev/null
+++ b/src/docbkx/reference/programming-model/programming-model.xml
@@ -0,0 +1,22 @@
+
+
+
+ Programming model for Spring Data Graph
+
+ This chapter covers the fundamentals of the programming model behind Spring Data Graph. It discusses the
+ AspectJ features used and the annotations provided by Spring Data Graph and how to use them.
+ Examples for this section are taken from the imdb project of
+ Spring Data Graph examples.
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/docbkx/reference/programming-model/projection.xml b/src/docbkx/reference/programming-model/projection.xml
new file mode 100644
index 000000000..6d6a7068e
--- /dev/null
+++ b/src/docbkx/reference/programming-model/projection.xml
@@ -0,0 +1,44 @@
+
+
+
+ Dynamic typing - Projection to unrelated, fitting types
+
+ As the underlying data model of a graph database doesn't imply and enforce strict type constraints like a
+ relational model does, it offers much more flexibility on how to model your domain classes and which of
+ those to use in different contexts.
+
+
+ For instance an order can be used in these contexts: customer, procurement, logistics, billing, fulfillment
+ and many more. Each of those contexts requires its distinct set of attributes and operations. As Java
+ doesn't support mixins one would put the sum of all of those into the entity class and thereby making it
+ very big, brittle and hard to understand. Being able to take a basic order and project it to a different
+ (not related in the inheritance hierarchy or even an interface) order type that is valid in the current
+ context and only offers the attributes and methods needed here would be very benefitial.
+
+ Spring Data Graph offers initial support for projecting node and relationship entities to different target
+ types. All instances of this projected entity share the same backing node or relationship, so data changes are
+ reflected immediately.
+
+
+ This could for instance also be used to handle nodes of a traversal with a unified (simpler) type (e.g. for
+ reporting or auditing) and only project them to a concrete, more functional target type when the business
+ logic requires it.
+
+ trainings;
+}
+
+for (Person person : finder.findAllByProperyValue("occupation","developer")) {
+ Developer developer = person.projectTo(Developer.class);
+ if (developer.isJavaDeveloper()) {
+ trainInSpringData(developer.projectTo(Trainee.class));
+ }
+}
+]]>
+
+
diff --git a/src/docbkx/reference/programming-model/transactions.xml b/src/docbkx/reference/programming-model/transactions.xml
new file mode 100644
index 000000000..a6434dbb1
--- /dev/null
+++ b/src/docbkx/reference/programming-model/transactions.xml
@@ -0,0 +1,93 @@
+
+
+
+ Transactions in Spring Data Graph
+
+ Neo4j is a transactional datastore which only allows modifications within transaction boundaries and fullfills
+ the ACID properties. Reading from the store is also possible outside of transactions.
+
+
+ Spring Data Graph integrates with transaction managers configured using Spring. The simplest scenario of
+ just running the graph database uses a SpringTransactionManager provided by the Neo4j kernel to be used
+ with Spring's JtaTransactionManager.
+
+ Note: The explicit XML configuration given below is encoded in the Neo4jConfiguration
+ configuration bean that uses Spring's @Configuration functioanlity. This simplifies the configuration.
+ An example is shown further below.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+]]>
+
+ For scenarios running multiple transactional resources there are two options.
+ First of all you can have Neo4j participate in the externally set up transaction manager using the new
+ SpringProvider by enabling the configuration parameter for your graph database. Either via the spring config
+ or the configuration file (neo4j.properties).
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ ]]>
+
+ You can configure a stock XA transaction manager to be used with Neo4j and the other resources (e.g. Atomikos,
+ JOTM, App-Server-TM). For a bit less secure but fast 1 phase commit best effort, use the implementation coming
+ with Spring Data Graph (ChainedTransactionManager). It takes a list of transaction-managers as
+ constructor params and will handle them in order for transaction start and commit (or rollback) in the reverse
+ order.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+]]>
+