org.neo4j.api.core
Interface RelationshipType

All Known Implementing Classes:
DynamicRelationshipType

public interface RelationshipType

A relationship type is a mandatory property on all relationships that is used to navigate the node space. 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 Neo-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.

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 ) )
 {
        // ...
 }
 
Please note that in early 1.0 betas, you were required to supply an enum of RelationshipTypes to the EmbeddedNeo constructor, or register valid relationship types. This is no longer needed.

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 Summary
 String name()
          Returns the name of the relationship type.
 

Method Detail

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


Copyright © 2009 Neo4j. All Rights Reserved.