1
votes

I am creating unique neo4j relationships in java class based on no. of column values in database. Value of column "Interface_Name" will be assigned to each relationship.My Code :

while (rs.next()){
    String rel = rs.getString("Interface_Name");
    GraphDatabaseService graphDb = new EmbeddedGraphDatabase("D://My Graph");
    Transaction tx = graphDb.beginTx();     
    try {       
        RelationshipType rel = DynamicRelationshipType.withName(rel); **//Gives error since rel is string** 
        .....
        tx.success();
    }
}

How can i create relationship Types based on Column values in DB?Inside while loop Relationship Types should get created according to DB values.

1

1 Answers

1
votes

You can't create relationships without creating nodes. You'll need a start node and an end node. Also, don't create a new GraphDatabaseService for every column you encounter. Your code could be something like this:

GraphDatabaseService graphDb = new EmbeddedGraphDatabase("D://My Graph");
while (rs.next()){
    String rel = rs.getString("Interface_Name");
    try (Transaction tx = graphDb.beginTx()) {
         RelationshipType relType = DynamicRelationshipType.withName(rel);
         graphDb.createNode().createRelationshipTo(graphDb.createNode(), relType);
         tx.success();
    }
}