Database Reference
In-Depth Information
Both the
@RelatedTo
and
@RelatedToVia
and annotations can take
a
type
and optional
direction
element, specified to clarify whether relationships of
a particular direction (valid values are
INCOMING
,
BOTH
, and the default
OUTGOING
)
should be included in the collection of entities returned.
Note that for the
friends
property you need to specify the direction as
BOTH
, as shown
in the following snippet:
@RelatedTo(type = "IS_FRIEND_OF", direction = Direction.BOTH)
Set<User> friends;
Logically, a
friends
relationship is bidirectional; physically within Neo4j, however, re-
lationships are only stored in one direction. By specifying
BOTH
, you're telling Neo4j to
consider any
IS_FRIENDS_OF
relationships associated with the user, regardless of the
direction in which the physical relationship is defined.
9.3. Accessing and persisting entities
You've seen how you can use annotations to provide SDN with the basic information it
needstomapyourPOJOclasses totheunderlyinggraph-backed database without somuch
as a low-level node or relationship class in sight. This is a great step forward in having to
only deal with business-level concepts in your domain model code. The next logical ques-
tions are how you can interact with these entities and how you can load and save your
POJO entities to and from the graph database.
SDN offers a few options in this area, and we'll be focusing on two of them in this section,
namely the
Neo4jTemplate
class and the more general concept of Spring data repos-
itories. Before that, we'll look at the supporting Spring configuration, which you need to
supply in order to make this all work.
9.3.1. Supporting Spring configuration
The following listing shows the minimum XML Spring configuration required to initialize
SDN so it can find your domain entities and make various beans (such as the
Neo4jTem-
plate
) available to you.