我试图根据2个节点之间路径长度2的传递关系添加新的关系,所有这些关系都是相同的类型(:TRUST)。我正在使用Java API并得到类似下面的工作,但似乎新添加的关系最终会出现在我的结果迭代器中,即使它在执行查询时并不存在。无论如何,我可以避免它吗?
private final String FIND_TRANSITIVE_RELATION_QUERY =
"MATCH (a:" + TwitterLabels.USERS + ") " +
"- [ab:" + TwitterRelationships.TRUST + "] -> " +
"(b:" + TwitterLabels.USERS + ") " +
"- [bc:" + TwitterRelationships.TRUST + "] -> " +
"(c:" + TwitterLabels.USERS + ")\n" +
"WHERE a <> c\n" +
"return ab, bc";
public void insertSimpleTransitiveTrust(){
try ( Transaction tx = graphDb.beginTx() )
{
Map<String, Object> params = new HashMap<String, Object>();
Result result = graphDb.execute( FIND_TRANSITIVE_RELATION_QUERY, params );
while(result.hasNext()){
Map<String, Object> map = result.next();
Object object = map.get("ab");
Relationship trustAb = (Relationship) object;
object = map.get("bc");
Relationship trustBc = (Relationship) object;
Node nodeA = trustAb.getStartNode();
Node nodeB = trustAb.getEndNode();
Node nodeC = trustBc.getEndNode();
Relationship transitiveTrust = nodeA.createRelationshipTo(nodeC, TwitterRelationships.TRUST);
transitiveTrust.setProperty("transitiveTrust", trustBc.getPropety(TwitterProperties.CONVERSATIONAL_TRUST));
}
tx.success();
}
答案 0 :(得分:0)
迭代和修改都在同一个事务中运行。这就是你看到自己修改的原因。
要防止在另一个线程中的另一个事务中执行修改,请使用该方法查看来自apoc过程的代码片段:https://github.com/neo4j-contrib/neo4j-apoc-procedures/blob/master/src/main/java/apoc/periodic/Periodic.java#L230-L260