Skip to content

Commit 7adf1c7

Browse files
GH-2279 - Add documentation.
1 parent def45d2 commit 7adf1c7

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/main/asciidoc/appendix/custom-queries.adoc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,9 @@ One way to do this are correlated subqueries (Neo4j 4.1+ required).
366366
== Spring Expression Language in custom queries
367367

368368
{spring-framework-ref}/core.html#expressions[Spring Expression Language (SpEL)] can be used in custom queries inside `:#{}`.
369+
The colon here refers to a parameter and such an expression should be used where parameters make sense.
370+
However, when using our <<literal-extension,literal extension>> you can use SpEL expression in places where standard Cypher
371+
won't allow parameters (such as for labels or relationship types).
369372
This is the standard Spring Data way of defining a block of text inside a query that undergoes SpEL evaluation.
370373

371374
The following example basically defines the same query as above, but uses a `WHERE` clause to avoid even more curly braces:
@@ -416,6 +419,7 @@ The `literal` extension can be used to make things like labels or relationship-t
416419
Neither labels nor relationship types can be parameterized in Cypher, so they must be given literal.
417420

418421
[source,java]
422+
[[literal-extension]]
419423
.literal-Extension
420424
----
421425
interface BaseClassRepository extends Neo4jRepository<Inheritance.BaseClass, Long> {
@@ -430,3 +434,42 @@ Here, the `literal` value has been used to match dynamically on a Label.
430434
If you pass in `SomeLabel` as a parameter to the method, `MATCH (n:``SomeLabel``) RETURN n`
431435
will be generated. Ticks have been added to correctly escape values. SDN won't do this
432436
for you as this is probably not what you want in all cases.
437+
438+
=== Referring to Labels
439+
440+
You already know how to map a Node to a domain object:
441+
442+
[source,java]
443+
.A Node with many labels
444+
----
445+
@Node(primaryLabel = "Bike", labels = {"Gravel", "Easy Trail"})
446+
public class BikeNode {
447+
@Id String id;
448+
449+
String name;
450+
}
451+
----
452+
453+
This node has a couple of labels, and it would be rather error prone to repeat them all the time in custom queries: You might
454+
forget one or make a typo. We offer the following expression to mitigate this: `#{#staticLabels}`. Notice that this one does
455+
_not_ start with a colon! You use it on repository methods annotated with `@Query`:
456+
457+
[source,java,indent=0,tabsize=4]
458+
.`#{#staticLabels}` in action
459+
----
460+
public interface BikeRepository extends Neo4jRepository<Bike, String> {
461+
462+
@Query("MATCH (n:#{#staticLabels}) WHERE n.id = $nameOrId OR n.name = $nameOrId RETURN n")
463+
Optional<Bike> findByNameOrId(@Param("nameOrId") String nameOrId);
464+
}
465+
----
466+
467+
This query will resolve to
468+
469+
[source,cypher]
470+
----
471+
MATCH (n:`Bike`:`Gravel`:`Easy Trail`) WHERE n.id = $nameOrId OR n.name = $nameOrId RETURN n
472+
----
473+
474+
Notice how we used standard parameter for the `nameOrId`: In most cases there is no need to complicate things here by
475+
adding a SpEL expression.

src/test/java/org/springframework/data/neo4j/integration/imperative/RepositoryIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
import org.springframework.data.neo4j.config.AbstractNeo4jConfig;
8282
import org.springframework.data.neo4j.core.DatabaseSelection;
8383
import org.springframework.data.neo4j.core.DatabaseSelectionProvider;
84+
import org.springframework.data.neo4j.core.Neo4jClient;
8485
import org.springframework.data.neo4j.core.Neo4jTemplate;
8586
import org.springframework.data.neo4j.core.convert.Neo4jConversions;
8687
import org.springframework.data.neo4j.core.mapping.Neo4jMappingContext;

src/test/java/org/springframework/data/neo4j/repository/query/Neo4jSpelSupportTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,7 @@ void shouldReplaceStaticLabels() {
133133
@Node(primaryLabel = "Bike", labels = {"Gravel", "Easy Trail"})
134134
static class BikeNode {
135135
@Id String id;
136+
137+
String name;
136138
}
137139
}

0 commit comments

Comments
 (0)