The Neo4jTemplate offers the convenient API of Spring templates for the Neo4j graph
database.
For direct retrieval of nodes and relationships, the getReferenceNode(),
getNode() and getRelationship() methods can be used.
There are methods (createNode() and createRelationship()) for creating nodes and
relationships that automatically set provided properties.
Example 23.1. Neo4j template
// TODO auto-post-construct !! Neo4jOperations neo = new Neo4jTemplate(graphDatabase).postConstruct(); Node mark = neo.createNode(map("name", "Mark")); Node thomas = neo.createNode(map("name", "Thomas")); neo.createRelationshipBetween(mark, thomas, "WORKS_WITH", map("project", "spring-data")); neo.index("devs", thomas, "name", "Thomas"); // Cypher TODO assertEquals( "Mark", neo.query("start p=node({person}) match p<-[:WORKS_WITH]-other return other.name", map("person", asList(thomas.getId()))).to(String.class).single()); // Gremlin assertEquals(thomas, neo.execute("g.v(person).out('WORKS_WITH')", map("person", mark.getId())).to(Node.class).single()); // Index lookup assertEquals(thomas, neo.lookup("devs", "name", "Thomas").to(Node.class).single()); // Index lookup with Result Converter assertEquals("Thomas", neo.lookup("devs", "name", "Thomas").to(String.class, new ResultConverter<PropertyContainer, String>() { public String convert(PropertyContainer element, Class<String> type) { return (String) element.getProperty("name"); } }).single());
All querying methods of the template return a uniform result type: Result<T>
which is also an Iterable<T>. The query result offers methods of converting each
element to a target type result.to(Type.class) optionally supplying a
ResultConverter<FROM,TO> which takes care of custom conversions. By default most
query methods can already handle conversions from and to: Paths, Nodes, Relationship and GraphEntities
as well as conversions backed by registered ConversionServices. A converted Result<FROM> is an
Iterable<TO>. Results can be limited to a single value using the result.single()
method. It also offers support for a pure callback function using a Handler<T>.
Adding nodes and relationships to an index is done with the index() method.
The lookup() 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. All lookup()
methods return a Result<PropertyContainer> to be used or transformed.
The traversal methods are at the core of graph operations.
The traverse() method covers the full traversal operation that takes a
TraversalDescription (typically built with the Traversal.description()
DSL) and runs it from the given start node. traverse returns a Result<Path>
to be used or transformed.
The Neo4jTemplate also allows execution of arbitrary Cypher queries. Via the query
methods the statement and parameter-Map are provided. Cypher Queries return tabular results, so the
Result<Map<String,Object>> contains the rows which can be either used as they are
or converted as needed.
Gremlin Scripts can run with the execute method, which also takes the parameters that will be
available as variables inside the script. The result of the executions is a generic
Result<Object> fit for conversion or usage.
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.