Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 871
reperion Avatar asked Oct 28 '25 12:10

reperion


2 Answers

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.

like image 125
Stefan Armbruster Avatar answered Oct 30 '25 15:10

Stefan Armbruster


Although definitely too late for the poster, this thread was one of the first to come up when searching for it. In newer Versions you can parameterize searching for a specific label:

@Query("MATCH (n:`:#{literal(#label)}`) RETURN n") 
List<BaseClass> findByLabel(String label);

Source: https://docs.spring.io/spring-data/neo4j/reference/appendix/custom-queries.html#literal-extension

like image 44
lingulata Avatar answered Oct 30 '25 14:10

lingulata