Creating Neo4j Relationships in java

送分小仙女□ 提交于 2019-12-11 11:22:41

问题


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:


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();
    }
}


来源:https://stackoverflow.com/questions/22928343/creating-neo4j-relationships-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!