Search code examples
neo4jcypherspring-data-neo4j

Is it possible cypher dynamic label at @Query?


I'm using SDN 3.1.0.RELEASE. And i trying danamic label query like

@Query("MATCH (n:{0}) RETURN n")
public List<SimpleArticle> findAllByDomain(String domain);

and give String parameter like SimpleArticle.class.getSimpleName() when i launched test code, i met "SyntaxException At {0}"

so i change query to

@Query("MATCH (n:`{0}`) RETURN n")

this code is work but don't find Domain node.
result log is "Executing remote cypher query: MATCH (n:`{0}`) RETURN n params {0=SimpleArticle}"

so i run this cypher query in query broswer

MATCH (n:`SimpleArticle`) RETURN n ; 

It works and find node.

Can i use dynamic labels in @Query ?


Solution

  • Labels cannot be parameterized. The rationale for this is that different labels might result in different query plans. A parameterized query is always using the same query plan - therefore it's not possible.

    The only way to use "semi"-dynamic labels is by using string concatenation or by Cypher DSL.