Defining node entities
Node entities are declared using the @NodeEntity annotation. Relationship entities use
the @RelationshipEntity annotation.
@NodeEntity: The basic building block
The @NodeEntity annotation is used to turn a POJO class into an entity backed by a node
in the graph database. Fields on the entity are by default mapped to properties of the node. Fields
referencing other node entities (or collections thereof) are linked with relationships. If the
useShortNames attribute overridden to false, the property and relationship names will
have the class name of the entity prepended.
@NodeEntity annotations are inherited from super-types and interfaces. It is not necessary
to annotate your domain objects at every inheritance level.
If the partial attribute is set to true, this entity takes part in a cross-store setting,
where the entity lives in both the graph database and a JPA data source. See
for more information.
Entity fields can be annotated with @GraphProperty, @RelatedTo,
@RelatedToVia, @Indexed, @GraphId and
@GraphTraversal.
Simple node entity
@GraphProperty: Optional annotation for property fields
It is not necessary to annotate data 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. Spring Data Neo4j includes a
custom conversion factory that comes with converters for Enums and Dates.
Transient fields are not persisted.
Currently there is no support for handling arbitrary collections of primitive or convertable values.
Support for this will be added by the 1.1. release.
This annotation is typically used with cross-store persistence. When a node entity is configured
as partial, then all fields that should be persisted to the graph must be explicitly annotated
with @GraphProperty.
@Indexed: Making entities searchable by field value
The @Indexed annotation can be declared on fields that are intended to be indexed by the Neo4j
indexing facilities. The resulting index can be used to later retrieve nodes or relationships
that contain a certain property value, e.g. a name. Often an index is used to establish the start
node for a traversal. Indexes are accessed by a repository for a particular node or relationship
entity type. See and
for more information.
@Query: fields as query result views
The @Query annotation leverages the delegation infrastructure used by the
Spring Data Neo4j aspects. It provides dynamic fields which, when accessed, return the values
selected by the provided query language expression. The provided query must contain a placeholder named %start
for the id of the current entity. For instance start n=(%start) match n-[:FRIEND]->friend return friend.
Graph queries can return variable number of entities. That's why annotation can be put onto fields
with a single value, an Iterable of a concrete type or an Iterable of Map<String,Object>.
Additional parameters are taken from the params attribute of the @Query annotation.
The tuples form key-value pairs that are provided to the query at execution time.
@Graph on a node entity field
(friend) return friend",
params = {"relType", "FRIEND"})
private Iterable friends;
}
]]>
Please note that this annotation can also be used on repository methods.
@GraphTraversal: fields as traversal result views
The @GraphTraversal annotation leverages the delegation infrastructure used by the
Spring Data Neo4j aspects. It provides dynamic fields which, when accessed, return an Iterable
of node entities that are the result of a traversal starting at the entity containing the field.
The TraversalDescription used for this is created by the
FieldTraversalDescriptionBuilder class defined by the traversalBuilder
attribute. The class of the resulting node entities must be provided with the
elementClass attribute.
@GraphTraversal from a node entity
people;
private static class PeopleTraversalBuilder implements FieldTraversalDescriptionBuilder {
@Override
public TraversalDescription build(NodeBacked start, Field field, String... params) {
return new TraversalDescriptionImpl()
.relationships(DynamicRelationshipType.withName(params[0]))
.filter(Traversal.returnAllButStartNode());
}
}
}
]]>