Database Reference
In-Depth Information
The createRelationshipBetween method takes four arguments: the node of the current user, the node of the user
being followed, the String value of the relationship type, and, optionally, any properties that need to be added the
Relationship being created. In this specific example, you also will use the getNode method in neo4jTemplate, which
takes an argument of the node ID and returns a Node object.
Once the operation is completed, the controller requests the
following
method in
UserImpl
to return the full list
of followers ordered by the username.
The “unfollow” operation for the
FOLLOWS
relationships uses a nearly identical application flow as “follows”. In the
unfollow
method, shown in Listing 11-32, the first step is to use the
getByUsername
to find each node in the expected
relationship. To remove the relationship, call deleteRelationshipBetween with three arguments—both nodes in the
relationship and the String value of the relationship. Once completed, the
UserController
executes the
following
method and returns the current list of users being followed.
Listing 11-32.
The
unfollow
method in
UserImpl
// UserImpl
public void
unfollow
(String currentusername, String username) throws Exception {
User cu =
getByUserName
(currentusername);
User toUnfollow =
getByUserName
(username);
neo4jTemplate.deleteRelationshipBetween(
cu, toUnfollow, GraphStoryConstants.FOLLOWS
);
}
User-Generated Content
Another important feature in social media applications is being able to have users view, add, edit, and remove
content—sometimes referred to as
user-generated content
. In the case of this content, you will not be creating
connections between the content and its owner but creating a linked list of status updates. In other words, you are
connecting a User to their most recent status update and then connecting each subsequent status to the next update
through the
CURRENTPOST
and
NEXTPOST
directed relationship types, respectively.
This approach is used for two reasons. First, the sample application displays a given number of posts at a time.
A modeled list is more efficient than getting all status updates connected to a user and then sorting and limiting the
return. With this relationship approach, you also help to limit the number of relationships that are placed on the User
and Content entities. Overall, the graph operations should be more efficient using this approach.
As shown in Listing 11-33, the
User
is tied the
Content
object through the
CURRENTPOST
relationship type as well
as a
Content
object connected to another
Content
object through the
NEXTPOST
.
Listing 11-33.
The
Content
entity
@NodeEntity
@TypeAlias("Content")
public class
Content
{
@GraphId
private Long nodeId;
@Indexed
private String contentId;
private String title;