2014-04-08 91 views
1

我正在基於no的java類中創建獨特的neo4j關係。數據庫中的列值。列 價值「接口」將被分配給每個relationship.My代碼:在java中創建Neo4j關係

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

我怎樣才能創建一個基於數據庫列值的關係類型,而內循環關係類型應根據DB值獲得創建?

回答

1

如果不創建節點,則無法創建關係。你需要一個開始節點和一個結束節點。另外,請勿爲您遇到的每一列創建新的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(); 
    } 
} 
+0

好吧,但我不能定義相同的「rel」變量第二次出現錯誤。如何解決它?請指導 – ashwini

+0

什麼是確切的例外信息? – tstorms

+0

在RelationshipType中重命名「rel」變量rel = .... //此行爲 ,因爲我已經定義了String rel = rs.next()。getString(「Interface_Name」); – ashwini