简体   繁体   English

在Java中创建Neo4j关系

[英]Creating Neo4j Relationships in java

I am creating unique neo4j relationships in java class based on no. 我正在基于no在java类中创建唯一的neo4j关系。 of column values in database. 数据库中的列值。 Value of column "Interface_Name" will be assigned to each relationship.My Code : “ Interface_Name”列的值将分配给每个关系。我的代码:

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. 我如何根据DB中的列值创建关系类型?while循环关系类型应根据DB值创建。

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. 另外,不要为遇到的每个列创建一个新的GraphDatabaseService 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();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM