我有用户1和7,我想合并他们之间的关系朋友。密码查询工作正常,但当我尝试在java中编写它不起作用
如何使用java执行此cypher查询:
Sidekiq.configure_server do |config|
config.redis = { size: 20 }
end
我试过这个,但它无法正常工作
MATCH (a:user), (b:user)
WHERE a.ID="1" AND b.ID="7"
Merge (a)-[:friend]->(b)
return a, b
我也试过这个
Result result = db.execute( "MERGE (a:user {ID:'7'})-[:friend]->(b:user {ID:'5'}) return a.ID,b.ID") )
两者都不起作用
答案 0 :(得分:0)
您有几个Java语法错误。
)
。它还需要以分号结尾。答案 1 :(得分:0)
以下是适合您案例的完整代码段:
try (Transaction tx = db.beginTx()) {
// Query, with params
String query = "MATCH (a:user {ID: {aId}}), (b:user {ID: {bId}}) " +
"MERGE (a)-[:friend]->(b) " +
"RETURN a, b";
// Params
Map<String, Object> params = new HashMap<>();
params.put("aId", 1);
params.put("bId", 7);
// Execute in try-with-resource, to ensure that result will be closed
try (Result result = db.execute(query, params)) {
// read result here
}
tx.success();
}
值得注意的是:
{aId}
和{bId}
Map
,查询到数据库您可能应该查看Using Neo4j from Java开发人员资源,以获取有关该主题的更多示例和信息。