Interface RelationshipType


public interface RelationshipType
A relationship type is mandatory on all relationships and is used to navigate the graph. RelationshipType is in particular a key part of the traverser framework but it's also used in various relationship operations on Node.

Relationship types are declared by the client and can be handled either dynamically or statically in a Neo4j-based application. Internally, relationship types are dynamic. This means that every time a client invokes node.createRelationshipTo(anotherNode, newRelType) and passes in a new relationship type then the new type will be transparently created. So instantiating a RelationshipType instance will not create it in the underlying storage, it is persisted only when the first relationship of that type is created.

However, in case the application does not need to dynamically create relationship types (most don't), then it's nice to have the compile-time benefits of a static set of relationship types. Fortunately, RelationshipType is designed to work well with Java 5 enums. This means that it's very easy to define a set of valid relationship types by declaring an enum that implements RelationshipType and then reuse that across the application. For example, here's how you would define an enum to hold all your relationship types:

 
 enum MyRelationshipTypes implements RelationshipType
 {
     CONTAINED_IN, KNOWS
 }
 
 
Then later, it's as easy to use as:
 
 node.createRelationshipTo( anotherNode, MyRelationshipTypes.KNOWS );
 for ( Relationship rel : node.getRelationships( MyRelationshipTypes.KNOWS ) )
 {
     // ...
 }
 
 

It's very important to note that a relationship type is uniquely identified by its name, not by any particular instance that implements this interface. This means that the proper way to check if two relationship types are equal is by invoking equals() on their names, NOT by using Java's identity operator (==) or equals() on the relationship type instances. A consequence of this is that you can NOT use relationship types in hashed collections such as HashMap and HashSet.

However, you usually want to check whether a specific relationship instance is of a certain type. That is best achieved with the Relationship.isType method, such as:

 
 if ( rel.isType( MyRelationshipTypes.CONTAINED_IN ) )
 {
     ...
 }
 
 
  • Method Details

    • name

      String name()
      Returns the name of the relationship type. The name uniquely identifies a relationship type, i.e. two different RelationshipType instances with different object identifiers (and possibly even different classes) are semantically equivalent if they have equal names.
      Returns:
      the name of the relationship type
    • withName

      static RelationshipType withName(String name)
      Instantiates a new RelationshipType with the given name.
      Parameters:
      name - the name of the dynamic relationship type
      Returns:
      a RelationshipType with the given name
      Throws:
      IllegalArgumentException - if name is null