在 Java 中创建 Neo4j 关系 [英] Creating Neo4j Relationships in java

查看:64
本文介绍了在 Java 中创建 Neo4j 关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于 no 在 java 类中创建独特的 neo4j 关系.数据库中的列值.Interface_Name"列的值将分配给每个关系.我的代码:

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.

推荐答案

不创建节点就无法创建关系.您将需要一个开始节点和一个结束节点.另外,不要为遇到的每一列都创建一个新的 GraphDatabaseService.您的代码可能是这样的:

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

这篇关于在 Java 中创建 Neo4j 关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆