env:
- Neo4j-4.4.x
- 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浏览器仍然可以找到目标关系,说明该方法已执行但未生效。
2条答案
按热度按时间oxcyiej71#
您的代码生成的查询在逻辑上不正确:
在这个查询中,您将整个
Neo4j Relationship
对象与一个查询对象进行比较,如下所示:这将不起作用,因为两个原因:
relation_prop
不是有效的relation
属性。1.还有很多其他的内部属性,关系的一部分。
在对
Neo4j Browser
运行的查询中,您只检查year
属性的值。要修复它,您可以尝试以下操作:bbmckpt72#
你可以用很多例子来检查official Neo4J java documentation。尤其是这一个:
并使您的最小用例更轻,删除任何无用的代码。