我正在基于 no 的 java 类中创建独特的 neo4j 关系。数据库中的列值。 “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();
}
}
如何根据数据库中的列值创建关系类型?在 while 循环内,应根据数据库值创建关系类型。
最佳答案
如果不创建节点,就无法创建关系。您将需要一个起始节点和一个结束节点。另外,不要为遇到的每个列创建新的 GraphDatabaseService
。您的代码可能是这样的:
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();
}
}
关于java - 在 java 中创建 Neo4j 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22928343/