Spring Data Neo4j

org.springframework.data.neo4j.support
Class Neo4jTemplate

java.lang.Object
  extended by org.springframework.data.neo4j.support.Neo4jTemplate
All Implemented Interfaces:
EntityPersister, Neo4jOperations

public class Neo4jTemplate
extends Object
implements Neo4jOperations, EntityPersister

Mediator class for the graph related services like the GraphDatabaseService, the used TypeRepresentationStrategy, entity instantiators for nodes and relationships as well as a spring conversion service.

It delegates the appropriate methods to those services. The services are not intended to be accessible from outside.

Since:
13.09.2010
Author:
Michael Hunger

Constructor Summary
Neo4jTemplate()
          default constructor for dependency injection, TODO provide dependencies at creation time
Neo4jTemplate(GraphDatabase graphDatabase)
           
Neo4jTemplate(GraphDatabase graphDatabase, PlatformTransactionManager transactionManager)
           
Neo4jTemplate(MappingInfrastructure infrastructure)
           
 
Method Summary
 org.neo4j.graphdb.Transaction beginTx()
          Delegates to GraphDatabaseService
<T> Result<T>
convert(Iterable<T> iterable)
          Converts the Iterable into a Result object for uniform handling.
<T> T
convert(Object value, Class<T> type)
          Converts a single object according to the configured ResultConverter of the Neo4j-Template.
<T> long
count(Class<T> entityClass)
          Provies the instance count a given entity type using the typerepresentation strategy configured for this template.
<S extends org.neo4j.graphdb.PropertyContainer,T>
T
createEntityFromState(S state, Class<T> type, MappingPolicy mappingPolicy)
           
<S extends org.neo4j.graphdb.PropertyContainer,T>
T
createEntityFromStoredType(S state, MappingPolicy mappingPolicy)
           
 org.neo4j.graphdb.Node createNode()
          Delegates to GraphDatabaseService
 org.neo4j.graphdb.Node createNode(Map<String,Object> properties)
          Creates a node
<T> T
createNodeAs(Class<T> target, Map<String,Object> properties)
          Creates a node mapped by the given entity class
 org.neo4j.graphdb.Relationship createRelationshipBetween(org.neo4j.graphdb.Node startNode, org.neo4j.graphdb.Node endNode, String relationshipType, Map<String,Object> properties)
          Creates a relationship with the given initial properties.
<R> R
createRelationshipBetween(Object start, Object end, Class<R> relationshipEntityClass, String relationshipType, boolean allowDuplicates)
          Creates a single relationship entity between two node entities with the given relationship type projected to the provided relationship entity class.
 void delete(Object entity)
          Removes the given node or relationship entity or node or relationship from the graph, the entity is first removed from all indexes and then deleted.
 void deleteRelationshipBetween(Object start, Object end, String type)
          Removes the relationship of this type between the two node entities
<T> T
exec(GraphCallback<T> callback)
          Executes the callback in a NON-transactional context.
 Result<Object> execute(String statement, Map<String,Object> params)
          Executes the given Gremlin statement and returns the result packaged as Result as Neo4j types, not Gremlin types.
<T> T
fetch(T value)
           
<T> org.neo4j.helpers.collection.ClosableIterable<T>
findAll(Class<T> entityClass)
          Provides all instances of a given entity type using the typerepresentation strategy configured for this template.
<T> T
findOne(long id, Class<T> entityClass)
          Retrieves a node or relationship and returns it mapped to the appropriate type
 ConversionService getConversionService()
           
 ResultConverter getDefaultConverter()
           
 EntityStateHandler getEntityStateHandler()
           
 GraphDatabase getGraphDatabase()
           
 org.neo4j.graphdb.GraphDatabaseService getGraphDatabaseService()
           
<T extends org.neo4j.graphdb.PropertyContainer>
org.neo4j.graphdb.index.Index<T>
getIndex(Class<?> indexedType, String propertyName)
          The index determined by the property of the indexed type is returned, so all the customization via @Indexed annotations is taken into consideration.
<S extends org.neo4j.graphdb.PropertyContainer,T>
org.neo4j.graphdb.index.Index<S>
getIndex(Class<T> type)
           
<S extends org.neo4j.graphdb.PropertyContainer,T>
org.neo4j.graphdb.index.Index<S>
getIndex(Class<T> type, String indexName, IndexType indexType)
           
<S extends org.neo4j.graphdb.PropertyContainer>
org.neo4j.graphdb.index.Index<S>
getIndex(Neo4jPersistentProperty property, Class<?> instanceType)
           
<S extends org.neo4j.graphdb.PropertyContainer>
org.neo4j.graphdb.index.Index<S>
getIndex(String name)
           
<T extends org.neo4j.graphdb.PropertyContainer>
org.neo4j.graphdb.index.Index<T>
getIndex(String indexName, Class<?> indexedType)
          Retrieves an existing index for the given class and/or name
 String getIndexKey(Neo4jPersistentProperty property)
           
 MappingInfrastructure getInfrastructure()
           
 MappingPolicy getMappingPolicy(Class<?> targetType)
           
 MappingPolicy getMappingPolicy(Object entity)
           
 org.neo4j.graphdb.Node getNode(long id)
          Delegates to the GraphDatabase
<S extends org.neo4j.graphdb.PropertyContainer>
S
getPersistentState(Object entity)
          just sets the persistent state (i.e.
 org.neo4j.graphdb.Node getReferenceNode()
          Returns the reference node.
 org.neo4j.graphdb.Relationship getRelationship(long id)
          Delegates to the GraphDatabase
<R> R
getRelationshipBetween(Object start, Object end, Class<R> relationshipEntityClass, String relationshipType)
          Retrieves a single relationship entity between two node entities with the given relationship type projected to the provided relationship entity class
 org.neo4j.graphdb.Relationship getRelationshipBetween(Object start, Object end, String relationshipType)
          Retrieves a single relationship entity between two node entities.
 javax.validation.Validator getValidator()
           
<T extends org.neo4j.graphdb.PropertyContainer>
T
index(String indexName, T element, String field, Object value)
          Indexes the given field and value for the element.
 boolean isManaged(Object entity)
           
 boolean isNodeEntity(Class<?> targetType)
           
 boolean isRelationshipEntity(Class<?> targetType)
           
<S extends org.neo4j.graphdb.PropertyContainer,T>
T
load(S state, Class<T> type)
           
<T extends org.neo4j.graphdb.PropertyContainer>
Result<T>
lookup(Class<?> indexedType, String propertyName, Object value)
          The value or query-object is looked up in the index indicated by the property of the indexed type, so all the customization via @Indexed annotations is taken into consideration.
<T extends org.neo4j.graphdb.PropertyContainer>
Result<T>
lookup(String indexName, Object query)
          The query is executed on the index returning the IndexHits wrapped in a Result to be converted into Paths or Entities.
<T extends org.neo4j.graphdb.PropertyContainer>
Result<T>
lookup(String indexName, String field, Object value)
          The value is looked up in the Neo4j index returning the IndexHits wrapped in a Result to be converted into Paths or Entities.
 Neo4jTemplate postConstruct()
           
<S extends org.neo4j.graphdb.PropertyContainer,T>
void
postEntityCreation(S node, Class<T> entityClass)
          Deprecated. 
<T> T
projectTo(Object entity, Class<T> targetType)
          Projects a node or relationship entity to a different type.
<T> T
projectTo(Object entity, Class<T> targetType, MappingPolicy mappingPolicy)
           
 Result<Map<String,Object>> query(String statement, Map<String,Object> params)
          Runs the given cypher statement and packages the result in a Result, simple conversions via the registered converter-factories are already executed via this method.
 Object query(String statement, Map<String,Object> params, org.springframework.data.util.TypeInformation<?> typeInformation)
           
<T> QueryEngine<T>
queryEngineFor(QueryType type)
          Provides a cypher or gremlin query engine set up with a default entity converter.
 void removeNodeEntity(Object entity)
           
 void removeRelationshipEntity(Object entity)
           
<T> GraphRepository<T>
repositoryFor(Class<T> clazz)
           
<T> T
save(T entity)
          Stores the given entity in the graph, if the entity is already attached to the graph, the node is updated, otherwise a new node is created.
 void setInfrastructure(MappingInfrastructure infrastructure)
           
<S extends org.neo4j.graphdb.PropertyContainer,T>
T
setPersistentState(T entity, S state)
           
 boolean transactionIsRunning()
           
 DataAccessException translateExceptionIfPossible(Exception ex)
           
 org.neo4j.graphdb.traversal.TraversalDescription traversalDescription()
           
 Result<org.neo4j.graphdb.Path> traverse(org.neo4j.graphdb.Node startNode, org.neo4j.graphdb.traversal.TraversalDescription traversal)
          Traverses the graph starting at the given node with the provided traversal description.
<T> Iterable<T>
traverse(Object entity, Class<?> targetType, org.neo4j.graphdb.traversal.TraversalDescription traversalDescription)
           
 Result<org.neo4j.graphdb.Path> traverse(Object start, org.neo4j.graphdb.traversal.TraversalDescription traversal)
          Traverses the graph starting at the given node entity with the provided traversal description.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Neo4jTemplate

public Neo4jTemplate()
default constructor for dependency injection, TODO provide dependencies at creation time


Neo4jTemplate

public Neo4jTemplate(GraphDatabase graphDatabase,
                     PlatformTransactionManager transactionManager)
Parameters:
graphDatabase - the neo4j graph database
transactionManager - if passed in, will be used to create implicit transactions whenever needed

Neo4jTemplate

public Neo4jTemplate(GraphDatabase graphDatabase)

Neo4jTemplate

public Neo4jTemplate(MappingInfrastructure infrastructure)
Method Detail

repositoryFor

public <T> GraphRepository<T> repositoryFor(Class<T> clazz)
Specified by:
repositoryFor in interface Neo4jOperations

getIndex

public <S extends org.neo4j.graphdb.PropertyContainer,T> org.neo4j.graphdb.index.Index<S> getIndex(Class<T> type)

getIndex

public <S extends org.neo4j.graphdb.PropertyContainer> org.neo4j.graphdb.index.Index<S> getIndex(String name)

getIndex

public <S extends org.neo4j.graphdb.PropertyContainer,T> org.neo4j.graphdb.index.Index<S> getIndex(Class<T> type,
                                                                                                   String indexName,
                                                                                                   IndexType indexType)

transactionIsRunning

public boolean transactionIsRunning()
Returns:
true if a transaction manager is available and a transaction is currently running

findOne

public <T> T findOne(long id,
                     Class<T> entityClass)
Description copied from interface: Neo4jOperations
Retrieves a node or relationship and returns it mapped to the appropriate type

Specified by:
findOne in interface Neo4jOperations
Returns:
mapped entity or null

findAll

public <T> org.neo4j.helpers.collection.ClosableIterable<T> findAll(Class<T> entityClass)
Description copied from interface: Neo4jOperations
Provides all instances of a given entity type using the typerepresentation strategy configured for this template. This method is also provided by the appropriate repository.

Specified by:
findAll in interface Neo4jOperations

count

public <T> long count(Class<T> entityClass)
Description copied from interface: Neo4jOperations
Provies the instance count a given entity type using the typerepresentation strategy configured for this template. This method is also provided by the appropriate repository.

Specified by:
count in interface Neo4jOperations

createEntityFromStoredType

public <S extends org.neo4j.graphdb.PropertyContainer,T> T createEntityFromStoredType(S state,
                                                                                      MappingPolicy mappingPolicy)
Specified by:
createEntityFromStoredType in interface EntityPersister

createEntityFromState

public <S extends org.neo4j.graphdb.PropertyContainer,T> T createEntityFromState(S state,
                                                                                 Class<T> type,
                                                                                 MappingPolicy mappingPolicy)
Specified by:
createEntityFromState in interface EntityPersister

load

public <S extends org.neo4j.graphdb.PropertyContainer,T> T load(S state,
                                                                Class<T> type)
Specified by:
load in interface Neo4jOperations

projectTo

public <T> T projectTo(Object entity,
                       Class<T> targetType)
Description copied from interface: Neo4jOperations
Projects a node or relationship entity to a different type. This can be used to use the same, schema free data in different contexts.

Specified by:
projectTo in interface EntityPersister
Specified by:
projectTo in interface Neo4jOperations

projectTo

public <T> T projectTo(Object entity,
                       Class<T> targetType,
                       MappingPolicy mappingPolicy)
Specified by:
projectTo in interface EntityPersister

getPersistentState

public <S extends org.neo4j.graphdb.PropertyContainer> S getPersistentState(Object entity)
just sets the persistent state (i.e. Node or id) to the entity, doesn't copy any values/properties.

Specified by:
getPersistentState in interface Neo4jOperations
Type Parameters:
S -
Parameters:
entity -
Returns:

setPersistentState

public <S extends org.neo4j.graphdb.PropertyContainer,T> T setPersistentState(T entity,
                                                                              S state)

postEntityCreation

@Deprecated
public <S extends org.neo4j.graphdb.PropertyContainer,T> void postEntityCreation(S node,
                                                                                            Class<T> entityClass)
Deprecated. 


delete

public void delete(Object entity)
Description copied from interface: Neo4jOperations
Removes the given node or relationship entity or node or relationship from the graph, the entity is first removed from all indexes and then deleted.

Specified by:
delete in interface Neo4jOperations

removeNodeEntity

public void removeNodeEntity(Object entity)

removeRelationshipEntity

public void removeRelationshipEntity(Object entity)

createNode

public org.neo4j.graphdb.Node createNode()
Delegates to GraphDatabaseService

Specified by:
createNode in interface Neo4jOperations

createNode

public org.neo4j.graphdb.Node createNode(Map<String,Object> properties)
Description copied from interface: Neo4jOperations
Creates a node

Specified by:
createNode in interface Neo4jOperations
Parameters:
properties - the properties that should be initially set on the node

createNodeAs

public <T> T createNodeAs(Class<T> target,
                          Map<String,Object> properties)
Description copied from interface: Neo4jOperations
Creates a node mapped by the given entity class

Specified by:
createNodeAs in interface Neo4jOperations
Parameters:
target - mapped entity class or Node.class
properties - the properties that should be initially set on the node

beginTx

public org.neo4j.graphdb.Transaction beginTx()
Delegates to GraphDatabaseService


postConstruct

@PostConstruct
public Neo4jTemplate postConstruct()

isNodeEntity

public boolean isNodeEntity(Class<?> targetType)
Specified by:
isNodeEntity in interface EntityPersister

isRelationshipEntity

public boolean isRelationshipEntity(Class<?> targetType)
Specified by:
isRelationshipEntity in interface EntityPersister

save

public <T> T save(T entity)
Description copied from interface: Neo4jOperations
Stores the given entity in the graph, if the entity is already attached to the graph, the node is updated, otherwise a new node is created. Attached relationships will be cascaded. This method is also provided by the appropriate repository.

Specified by:
save in interface Neo4jOperations

isManaged

public boolean isManaged(Object entity)

query

public Object query(String statement,
                    Map<String,Object> params,
                    org.springframework.data.util.TypeInformation<?> typeInformation)

getRelationshipBetween

public <R> R getRelationshipBetween(Object start,
                                    Object end,
                                    Class<R> relationshipEntityClass,
                                    String relationshipType)
Description copied from interface: Neo4jOperations
Retrieves a single relationship entity between two node entities with the given relationship type projected to the provided relationship entity class

Specified by:
getRelationshipBetween in interface Neo4jOperations

getRelationshipBetween

public org.neo4j.graphdb.Relationship getRelationshipBetween(Object start,
                                                             Object end,
                                                             String relationshipType)
Description copied from interface: Neo4jOperations
Retrieves a single relationship entity between two node entities.

Specified by:
getRelationshipBetween in interface Neo4jOperations

deleteRelationshipBetween

public void deleteRelationshipBetween(Object start,
                                      Object end,
                                      String type)
Description copied from interface: Neo4jOperations
Removes the relationship of this type between the two node entities

Specified by:
deleteRelationshipBetween in interface Neo4jOperations

createRelationshipBetween

public <R> R createRelationshipBetween(Object start,
                                       Object end,
                                       Class<R> relationshipEntityClass,
                                       String relationshipType,
                                       boolean allowDuplicates)
Description copied from interface: Neo4jOperations
Creates a single relationship entity between two node entities with the given relationship type projected to the provided relationship entity class. If it allowDuplicates existing relationships won't be taken into account. Returns the projected relationship entity.

Specified by:
createRelationshipBetween in interface Neo4jOperations

createRelationshipBetween

public org.neo4j.graphdb.Relationship createRelationshipBetween(org.neo4j.graphdb.Node startNode,
                                                                org.neo4j.graphdb.Node endNode,
                                                                String relationshipType,
                                                                Map<String,Object> properties)
Description copied from interface: Neo4jOperations
Creates a relationship with the given initial properties.

Specified by:
createRelationshipBetween in interface Neo4jOperations

translateExceptionIfPossible

public DataAccessException translateExceptionIfPossible(Exception ex)

exec

public <T> T exec(GraphCallback<T> callback)
Description copied from interface: Neo4jOperations
Executes the callback in a NON-transactional context.

Specified by:
exec in interface Neo4jOperations
Type Parameters:
T - return type
Parameters:
callback - for executing graph operations NON-transactionally, not null
Returns:
whatever the callback chooses to return

getReferenceNode

public org.neo4j.graphdb.Node getReferenceNode()
Description copied from interface: Neo4jOperations
Returns the reference node.

Specified by:
getReferenceNode in interface Neo4jOperations

getNode

public org.neo4j.graphdb.Node getNode(long id)
Description copied from interface: Neo4jOperations
Delegates to the GraphDatabase

Specified by:
getNode in interface Neo4jOperations
Parameters:
id - node id
Returns:
the requested node of the underlying graph database

getRelationship

public org.neo4j.graphdb.Relationship getRelationship(long id)
Description copied from interface: Neo4jOperations
Delegates to the GraphDatabase

Specified by:
getRelationship in interface Neo4jOperations
Parameters:
id - relationship id
Returns:
the requested relationship of the underlying graph database

index

public <T extends org.neo4j.graphdb.PropertyContainer> T index(String indexName,
                                                               T element,
                                                               String field,
                                                               Object value)
Description copied from interface: Neo4jOperations
Indexes the given field and value for the element.

Specified by:
index in interface Neo4jOperations
Type Parameters:
T - the provided element type
Parameters:
indexName - Name of the index, will be checked against existing indexes according to the given element assumes a "node" node index or "relationship" relationship index for a null value
element - node or relationship to index
field - field to index
value - value to index
Returns:
the provided element for convenience

fetch

public <T> T fetch(T value)
Specified by:
fetch in interface Neo4jOperations

getMappingPolicy

public MappingPolicy getMappingPolicy(Class<?> targetType)
Specified by:
getMappingPolicy in interface EntityPersister
Specified by:
getMappingPolicy in interface Neo4jOperations

convert

public <T> Result<T> convert(Iterable<T> iterable)
Description copied from interface: Neo4jOperations
Converts the Iterable into a Result object for uniform handling. E.g. template.convert(node.getRelationships());

Specified by:
convert in interface Neo4jOperations

convert

public <T> T convert(Object value,
                     Class<T> type)
Description copied from interface: Neo4jOperations
Converts a single object according to the configured ResultConverter of the Neo4j-Template.

Specified by:
convert in interface Neo4jOperations

getDefaultConverter

public ResultConverter getDefaultConverter()
Specified by:
getDefaultConverter in interface Neo4jOperations

queryEngineFor

public <T> QueryEngine<T> queryEngineFor(QueryType type)
Description copied from interface: Neo4jOperations
Provides a cypher or gremlin query engine set up with a default entity converter.

Specified by:
queryEngineFor in interface Neo4jOperations

query

public Result<Map<String,Object>> query(String statement,
                                        Map<String,Object> params)
Description copied from interface: Neo4jOperations
Runs the given cypher statement and packages the result in a Result, simple conversions via the registered converter-factories are already executed via this method.

Specified by:
query in interface Neo4jOperations

execute

public Result<Object> execute(String statement,
                              Map<String,Object> params)
Description copied from interface: Neo4jOperations
Executes the given Gremlin statement and returns the result packaged as Result as Neo4j types, not Gremlin types. The Neo4j-Graph is provided as variable "g". Table rows are converted to Map.

Specified by:
execute in interface Neo4jOperations

traverse

public Result<org.neo4j.graphdb.Path> traverse(Object start,
                                               org.neo4j.graphdb.traversal.TraversalDescription traversal)
Description copied from interface: Neo4jOperations
Traverses the graph starting at the given node entity with the provided traversal description. The Path's of the traversal will be packaged into a Result which can be easily converted into Nodes, Relationships or Graph-Entities.

Specified by:
traverse in interface Neo4jOperations

traverse

public <T> Iterable<T> traverse(Object entity,
                                Class<?> targetType,
                                org.neo4j.graphdb.traversal.TraversalDescription traversalDescription)

traverse

public Result<org.neo4j.graphdb.Path> traverse(org.neo4j.graphdb.Node startNode,
                                               org.neo4j.graphdb.traversal.TraversalDescription traversal)
Description copied from interface: Neo4jOperations
Traverses the graph starting at the given node with the provided traversal description. The Path's of the traversal will be packaged into a Result which can be easily converted into Nodes, Relationships or Graph-Entities.

Specified by:
traverse in interface Neo4jOperations

lookup

public <T extends org.neo4j.graphdb.PropertyContainer> Result<T> lookup(String indexName,
                                                                        String field,
                                                                        Object value)
Description copied from interface: Neo4jOperations
The value is looked up in the Neo4j index returning the IndexHits wrapped in a Result to be converted into Paths or Entities.

Specified by:
lookup in interface Neo4jOperations

lookup

public <T extends org.neo4j.graphdb.PropertyContainer> Result<T> lookup(Class<?> indexedType,
                                                                        String propertyName,
                                                                        Object value)
Description copied from interface: Neo4jOperations
The value or query-object is looked up in the index indicated by the property of the indexed type, so all the customization via @Indexed annotations is taken into consideration. The resulting IndexHits are returned as a Result to be converted into Entities and other types.

Specified by:
lookup in interface Neo4jOperations

getIndex

public <T extends org.neo4j.graphdb.PropertyContainer> org.neo4j.graphdb.index.Index<T> getIndex(String indexName,
                                                                                                 Class<?> indexedType)
Description copied from interface: Neo4jOperations
Retrieves an existing index for the given class and/or name

Specified by:
getIndex in interface Neo4jOperations
Parameters:
indexName - might be null
indexedType - entity class, might be null
Returns:
Index<Node%gt; or Index<Relationship>

getIndex

public <T extends org.neo4j.graphdb.PropertyContainer> org.neo4j.graphdb.index.Index<T> getIndex(Class<?> indexedType,
                                                                                                 String propertyName)
Description copied from interface: Neo4jOperations
The index determined by the property of the indexed type is returned, so all the customization via @Indexed annotations is taken into consideration.

Specified by:
getIndex in interface Neo4jOperations

lookup

public <T extends org.neo4j.graphdb.PropertyContainer> Result<T> lookup(String indexName,
                                                                        Object query)
Description copied from interface: Neo4jOperations
The query is executed on the index returning the IndexHits wrapped in a Result to be converted into Paths or Entities.

Specified by:
lookup in interface Neo4jOperations

traversalDescription

public org.neo4j.graphdb.traversal.TraversalDescription traversalDescription()
Specified by:
traversalDescription in interface Neo4jOperations
Returns:
a traversal description suited for the current mode of operation (aware of remote REST connections)

getEntityStateHandler

public EntityStateHandler getEntityStateHandler()

getConversionService

public ConversionService getConversionService()

getValidator

public javax.validation.Validator getValidator()

getGraphDatabaseService

public org.neo4j.graphdb.GraphDatabaseService getGraphDatabaseService()

setInfrastructure

public void setInfrastructure(MappingInfrastructure infrastructure)

getInfrastructure

public MappingInfrastructure getInfrastructure()

getGraphDatabase

public GraphDatabase getGraphDatabase()
Specified by:
getGraphDatabase in interface Neo4jOperations
Returns:
the graph database used by the template

getIndexKey

public String getIndexKey(Neo4jPersistentProperty property)

getIndex

public <S extends org.neo4j.graphdb.PropertyContainer> org.neo4j.graphdb.index.Index<S> getIndex(Neo4jPersistentProperty property,
                                                                                                 Class<?> instanceType)

getMappingPolicy

public MappingPolicy getMappingPolicy(Object entity)

Spring Data Neo4j

Copyright © 2011 SpringSource. All Rights Reserved.