0
votes

I'm trying to parametrize the match part of a annotated Cypher query with Spring-Data-Neo4j-3.1.4 as follows. The first method is working. Passing the node type as parameter in the second method fails.

public interface NodeRepository extends CrudRepository<Node, Long> {

    @Query("START n=node(*) MATCH (n:Organization) RETURN n")
    List<Node> findByNodeType();

    @Query("START n=node(*) MATCH (n:{0}) RETURN n")
    List<Node> findByNodeType(String nodeType);
}

Exception is:

org.springframework.dao.InvalidDataAccessResourceUsageException: 
  Error executing statement START n=node(*) MATCH (n:{0}) RETURN n;     
  nested exception is org.springframework.dao.InvalidDataAccessResourceUsageException: 
  Error executing statement START n=node(*) MATCH (n:{0}) RETURN n; 
  nested exception is Invalid input '{': 
  expected whitespace or a label name (line 1, column 26)
  "START n=node(*) MATCH (n:{0}) RETURN n"
  at org.springframework.data.neo4j.support.query.CypherQueryEngineImpl.query(CypherQueryEngineImpl.java:61)
  at org.springframework.data.neo4j.repository.query.GraphRepositoryQuery.dispatchQuery(GraphRepositoryQuery.java:107) 
  at org.springframework.data.neo4j.repository.query.GraphRepositoryQuery$1.doWithGraph(GraphRepositoryQuery.java:89) 
  at org.springframework.data.neo4j.support.Neo4jTemplate.doExecute(Neo4jTemplate.java:457) 
  at org.springframework.data.neo4j.support.Neo4jTemplate.access$000(Neo4jTemplate.java:87) 
  at org.springframework.data.neo4j.support.Neo4jTemplate$2.doInTransaction(Neo4jTemplate.java:471)

How do pass the node as a parameter to the Cypher query?

2
Also change your queries to: MATCH (n:Organization) RETURN nMichael Hunger
You can try: MATCH (n) WHERE {0} IN labels(n) RETURN n but it won't be efficient, why would you want to do that in the first place ??Michael Hunger
MATCH (n) WHERE {0} IN labels(n) RETURN n is working. You should post this as an answer.uı6ʎɹnɯ ꞁəıuɐp

2 Answers

1
votes

Change your queries to: MATCH (n:Organization) RETURN n

You can try: MATCH (n) WHERE {0} IN labels(n) RETURN n

but it won't be efficient, why would you want to do that in the first place ?

Labels are not allowed as parameters (yet) for query-planner reasons.

0
votes

If you read the exception : Invalid input '{':

It means that the query is wrong. In fact you cannot pass labels as parameter in a Cypher query, so simple it is ;-)