Neo4j 2.2.2 和错误的 Cypher 查询

Neo4j 2.2.2 and wrong Cypher query

继续这个问题

我已经转移到 Neo4j 2.2.2,现在有一个 Cypher 查询有问题,该查询在以前的 Neo4j 2.1.7 上运行良好

我使用 Spring 数据 Neo4J:

@Query("MATCH (d:Decision) WHERE id(d) IN {decisionsIds} WITH d OPTIONAL MATCH (d)-[r]-(t) DELETE d, r WITH t, r OPTIONAL MATCH (t)-[r2:VOTED_ON|:CREATED_BY|:VOTED_FOR]-() WITH t, r2, r WHERE none(x in labels(t) WHERE x in ['User', 'Decision']) DELETE t, r2")
void deleteDecisions(@Param("decisionsIds") List<Long> decisionsIds);

错误是:

org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing statement MATCH (d:Decision) WHERE id(d) IN {decisionsIds} WITH d OPTIONAL MATCH (d)-[r]-(t) DELETE d, r WITH t, r OPTIONAL MATCH (t)-[r2:VOTED_ON|:CREATED_BY|:VOTED_FOR]-() WITH t, r2, r WHERE none(x in labels(t) WHERE x in ['User', 'Decision']) DELETE t, r2; nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: Error executing statement MATCH (d:Decision) WHERE id(d) IN {decisionsIds} WITH d OPTIONAL MATCH (d)-[r]-(t) DELETE d, r WITH t, r OPTIONAL MATCH (t)-[r2:VOTED_ON|:CREATED_BY|:VOTED_FOR]-() WITH t, r2, r WHERE none(x in labels(t) WHERE x in ['User', 'Decision']) DELETE t, r2; nested exception is org.neo4j.kernel.api.exceptions.EntityNotFoundException: Unable to load NODE with id 227.
    at org.springframework.data.neo4j.support.query.CypherQueryEngineImpl.query(CypherQueryEngineImpl.java:61)
    at org.springframework.data.neo4j.repository.query.GraphRepositoryQuery.dispatchQuery(GraphRepositoryQuery.java:108)
    at org.springframework.data.neo4j.repository.query.GraphRepositoryQuery.doWithGraph(GraphRepositoryQuery.java:90)
    at org.springframework.data.neo4j.support.Neo4jTemplate.doExecute(Neo4jTemplate.java:465)
    at org.springframework.data.neo4j.support.Neo4jTemplate.access[=11=]0(Neo4jTemplate.java:87)
    at org.springframework.data.neo4j.support.Neo4jTemplate.doInTransaction(Neo4jTemplate.java:479)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
    at org.springframework.data.neo4j.support.Neo4jTemplate.exec(Neo4jTemplate.java:476)
    at org.springframework.data.neo4j.repository.query.GraphRepositoryQuery.execute(GraphRepositoryQuery.java:84)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:431)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:409)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)

有什么问题吗?

已更新

调试后我发现 无法加载 ID 为 227 的节点 227 是一个 child 节点 ID.. 例如我有 ParentNode(id=201 ) 和一个 ChildNode(id=227)。我想删除他所有的 children 的 ParentNode。这样我需要传入 parent 节点和 child 节点的 {decisionsIds} ID(顺序为 [227, 201] - child 首先, parent 其次)现在的问题是 Cypher 查询无法删除 child 节点并出现以下错误 Unable to load NODE with id 227 .. 但为什么它在 Neo4j 中有效2.1.7 以及如何在 Neo4j 2.2.2

中修复它

[编辑#2]

附加信息非常有用。问题可能是 t 可以是已删除的子节点。

试试这个查询,它确保在初始删除后使用的 t 不是已删除的节点。

MATCH (d:Decision)
WHERE id(d) IN {decisionsIds}
OPTIONAL MATCH (d)-[r]-(t)
DELETE d, r
WITH t, r
WHERE NOT (id(t) IN {decisionsIds})
OPTIONAL MATCH (t)-[r2:VOTED_ON|:CREATED_BY|:VOTED_FOR]-()
WHERE r2 <> r
WITH t, r2
WHERE none(x in labels(t) WHERE x in ['User', 'Decision'])
DELETE t, r2;