neo4j 如何通过传递参数删除与属性的关系?

mo49yndu  于 2023-06-05  发布在  其他
关注(0)|答案(2)|浏览(259)

env:

  1. Neo4j-4.4.x
  2. neo4j-jdbc-driver 4.0.9
    想象一下,我执行下面的密码得到一个“教师”节点和一个“学生”节点,它们之间的关系是“教育”,并且关系有一个年份属性。
    密码:
merge (subject:Teacher{teacher_id: 'teacher_001'})  
merge (object:Student{student_id:'student_001'})  
merge (subject)-[relation:educate]->(object) set relation.year='3'  return subject,relation,object

图-1:

为什么我下面的方法在参数Map的帮助下不起作用?

@Test
public void testDeleteEdgeWithProperties_001() {
    Driver driver = GraphDatabase.driver("neo4j://localhost:7687", AuthTokens.basic("neo4j", "neo4j123"));
    Session session = driver.session();
    Transaction transaction = session.beginTransaction();

    String relationModelType = "educate";
    String subjectModelType = "Teacher";
    String subjectKey = "teacher_id";
    String subjectId = "teacher_001";
    String objectModelType = "Student";
    String objectKey ="student_id";
    String objectId = "student_001";

    Map<String, Object> properties = new HashMap<>();
    properties.put("year","3");
    Map<String, Object> params = new HashMap<>();
    params.put("relation_prop", properties == null ? new HashMap<>() : properties);

    // 删除
    String conditionDeleteCypher = " WHERE relation = $relation_prop";
    String cypher =  " MATCH (subject:" + subjectModelType + "{" + subjectKey + ": '" + subjectId + "'}) " +
            " MATCH (object:" + objectModelType + "{" + objectKey + ":'" + objectId + "'}) " +
            " MATCH (subject)-[relation:" + relationModelType +"]->(object) " +
            conditionDeleteCypher +
            " DELETE relation";

    System.out.println(cypher);
    transaction.run(cypher, params);
    transaction.commit();
    transaction.close();
}

通过上述方法打印的密码如下:

MATCH (subject:Teacher{teacher_id: 'teacher_001'})  MATCH (object:Student{student_id:'student_001'})  MATCH (subject)-[relation:educate]->(object)  WHERE relation = $relation_prop DELETE relation

然而,上面的密码可以在neo4j浏览器中成功执行(见下图2)。我这是怎么了感谢任何有用的回复
图2:

我尝试了几种方法来设置参数,但都不起作用。

String conditionDeleteCypher = " WHERE relation = $relation_prop";
String conditionDeleteCypher = " WHERE relation =+ $relation_prop";
String conditionDeleteCypher = " WHERE relation += $relation_prop";

我提供的@Test代码是完全可执行的,没有错误消息。执行后,neo4j浏览器仍然可以找到目标关系,说明该方法已执行但未生效。

oxcyiej7

oxcyiej71#

您的代码生成的查询在逻辑上不正确:

MATCH (subject:Teacher{teacher_id: 'teacher_001'})  
MATCH (object:Student{student_id:'student_001'})  
MATCH (subject)-[relation:educate]->(object)  WHERE relation = $relation_prop 
DELETE relation

在这个查询中,您将整个Neo4j Relationship对象与一个查询对象进行比较,如下所示:

{
  relation_prop: {
     year: "3"
  }
}

这将不起作用,因为两个原因:

  1. relation_prop不是有效的relation属性。
    1.还有很多其他的内部属性,关系的一部分。
    在对Neo4j Browser运行的查询中,您只检查year属性的值。要修复它,您可以尝试以下操作:
@Test
public void testDeleteEdgeWithProperties_001() {
    Driver driver = GraphDatabase.driver("neo4j://localhost:7687", AuthTokens.basic("neo4j", "neo4j123"));
    Session session = driver.session();
    Transaction transaction = session.beginTransaction();

    String relationModelType = "educate";
    String subjectModelType = "Teacher";
    String subjectKey = "teacher_id";
    String subjectId = "teacher_001";
    String objectModelType = "Student";
    String objectKey ="student_id";
    String objectId = "student_001";

    Map<String, Object> properties = new HashMap<>();
    properties.put("year","3");

    // 删除
    String conditionDeleteCypher = " WHERE relation.year = $year";
    String cypher =  " MATCH (subject:" + subjectModelType + "{" + subjectKey + ": '" + subjectId + "'}) " +
            " MATCH (object:" + objectModelType + "{" + objectKey + ":'" + objectId + "'}) " +
            " MATCH (subject)-[relation:" + relationModelType +"]->(object) " +
            conditionDeleteCypher +
            " DELETE relation";

    System.out.println(cypher);
    transaction.run(cypher, properties);
    transaction.commit();
    transaction.close();
}
bbmckpt7

bbmckpt72#

你可以用很多例子来检查official Neo4J java documentation。尤其是这一个:

Map<String,Object> params = new HashMap<>();
params.put( "name", "Johan" );

String query =
"MATCH (n:Person)" + "\n" +
"WHERE n.name = $name" + "\n" +
"RETURN n";

Result result = transaction.execute( query, params );

并使您的最小用例更轻,删除任何无用的代码。

相关问题