Database Reference
In-Depth Information
@NodeEntity
public class User {
...
@Query( value =
"match (n)-[r:IS_FRIEND_OF]-(friend)-[r2:IS_FRIEND_OF]-(fof) " +
"where id(n) = { self } " +
"return distinct fof")
Iterable<User> friendsOfFriends;
Whenever a User entity is loaded (either through Neo4jTemplate or UserRepos-
itory methods), the Cypher query specified in the annotation will be executed and the
result stored in the variable ( friendsOfFriends , in this case).
This is a standard Cypher query with the only new syntax being the reference to {self} ,
which refers to the current ID of the node backing this entity. This makes sense, as queries
defined on an entity should generally be related to that entity. If your query isn't specific-
ally related to the node entity in question, this is an indication that it probably needs to be
defined in a repository as a more generic method.
You can dynamically specify parameters to your query by supplying a set of key-value
pairs via the params attribute on the annotation. Following is another snippet illustrating
how this can be done to answer the query, “for a given a user, for each of his or her direct
friends, count the number of friends and return these counts.”
@Query(value =
"match (n)-[r]-(friend)-[r2:IS_FRIEND_OF]-(fof) " +
"where id(n) = {self} " +
"and type(r) = { friendRelName }" +
"return friend.name as friendName , count(fof) as numFriends",
params = { "friendRelName" ,"IS_FRIEND_OF"})
Iterable<Map<String,Object>> friendsOfFriendsCount;
As you're not returning the whole entity, but rather a subset of the data, the result of this
querywillbearead-onlycollection( Iterable )ofentries,withthekeysbeingthenames
of the variables defined in the return section of the query and their values being the result.
Using the example graph in figure 9.9 , running this query against John would result in the
following answer:
{friendName=Kate, numFriends=2}
{friendName=Jack, numFriends=2}
Search WWH ::




Custom Search