Database Reference
In-Depth Information
User Node Entity
We will begin the social graph model by reviewing the code for creating a User node in the graph via the sign-up
process. Later in this section, I will briefly review the code to validate a user upon attempting to login. In each case,
the code contains brief validation routines to demonstrate the basics of running checks against data. In the case of
sign-up, the code will check to see if a User already exists with the same username.
Node Entities
To begin, open the User class located in the com.practicalneo4j.graphstory.domain package. First, you should
notice that the User class—as with each Node Entity—is annotated with @NodeEntity , as shown in Listing 11-29. You
can also use the @TypeAlias to specify the alias name, which could be helpful if or when refactoring occurs. Each of
the entities should have a @GraphId annotation to set the property that will contain the node's ID.
Listing 11-16. The User Entity
@NodeEntity
@TypeAlias("User")
public class User {
@GraphId
private Long nodeId;
@Indexed
private String userId;
@Indexed
private String username;
private String firstname;
private String lastname;
@RelatedTo(type = GraphStoryConstants.MADE, direction = Direction.OUTGOING,
elementClass = Purchase.class)
private Purchase purchase;
@RelatedTo(type = GraphStoryConstants.USES, direction = Direction.OUTGOING,
elementClass = Tag.class)
private Set<Tag> tags;
@RelatedTo(type = GraphStoryConstants.HAS, direction = Direction.OUTGOING,
elementClass = Location.class)
private Set<Location> locations;
// getters and setters
}
Next, properties that require index lookups should use the @Indexed annotation. In most cases, simply setting
the annotation will suffice in order to do future lookups. However—as noted earlier in this chapter—it is also possible
to use the annotation property “unique=true” with @Indexed to help ensure no duplicates can be added to the index.
Node Entities in SDN are only allowed one unique index regardless of the type of index being used. In the case of a
unique index, the value is checked at creation time and re-uses the existing entity if the key-value pair already exists.
 
Search WWH ::




Custom Search