Neo4j 2.2.2 and wrong Cypher query

255 views Asked by At

In continue to this question Neo4j how to delete nodes recursively from some start node

I have moved to Neo4j 2.2.2 and now have a problem with one Cypher query that works fine on a previous Neo4j 2.1.7

I use Spring Data 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);

The error 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.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$1.doWithGraph(GraphRepositoryQuery.java:90)
    at org.springframework.data.neo4j.support.Neo4jTemplate.doExecute(Neo4jTemplate.java:465)
    at org.springframework.data.neo4j.support.Neo4jTemplate.access$000(Neo4jTemplate.java:87)
    at org.springframework.data.neo4j.support.Neo4jTemplate$2.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$1.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)

what can be wrong ?

UPDATED

After debugging I have figured out that Unable to load NODE with id 227 227 is a child node id.. for example I have ParentNode(id=201) with a one ChildNode(with id=227). I want to delete ParentNode with all of his children. This way I need to pass into {decisionsIds} IDs of parent node and child node(in order [227, 201] - child first, parent next) and right now the issue is that the Cypher query unable to delete the child node with the following error Unable to load NODE with id 227 .. but why does it work in Neo4j 2.1.7 and how to fix it in Neo4j 2.2.2

1

There are 1 answers

5
cybersam On BEST ANSWER

[EDIT #2]

The additional information is very helpful. The issue might to be that t can be a deleted child node.

Try this query, which makes sure that the t that is used after the initial deletions is not a deleted node.

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;