Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neo4j Dynamic Relationship Types, not with enums

Tags:

java

enums

neo4j

How would I take the string "KNOWS" and use it as a Relationship type instead of using an enum RelTypes.KNOWS ... I need to dynamically add relationship instead of using only the 2 enums RelTypes.KNOWS and RelTypes.IS_FRIENDS_WITH

// START SNIPPET: createReltype
private static enum RelTypes implements RelationshipType
{
    KNOWS,
    IS_FRIENDS_WITH
}
// END SNIPPET: createReltype

public static void main( final String[] args )
{
    // START SNIPPET: startDb
    GraphDatabaseService graphDb = new EmbeddedGraphDatabase( DB_PATH );
    registerShutdownHook( graphDb );
    // END SNIPPET: startDb

    // START SNIPPET: operationsInATransaction
    Transaction tx = graphDb.beginTx();
    try
    {
        Node john = graphDb.createNode();
        john.setProperty("name", "John" );
        Node george = graphDb.createNode();
        george.setProperty("name", "George" );

        firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );

        tx.success();
    }
    finally
    {
        tx.finish();
    }
    // END SNIPPET: removingData

    System.out.println( "Shutting down database ..." );
    // START SNIPPET: shutdownServer
    graphDb.shutdown();
    // END SNIPPET: shutdownServer
}
like image 964
RichardW Avatar asked Oct 10 '22 04:10

RichardW


1 Answers

Creating relationship types dynamically from a string is exactly what org.neo4j.graphdb.DynamicRelationshipType exists for.

like image 179
thobe Avatar answered Oct 13 '22 11:10

thobe