@@ -366,6 +366,9 @@ One way to do this are correlated subqueries (Neo4j 4.1+ required).
366
366
== Spring Expression Language in custom queries
367
367
368
368
{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).
369
372
This is the standard Spring Data way of defining a block of text inside a query that undergoes SpEL evaluation.
370
373
371
374
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
416
419
Neither labels nor relationship types can be parameterized in Cypher, so they must be given literal.
417
420
418
421
[source,java]
422
+ [[literal-extension]]
419
423
.literal-Extension
420
424
----
421
425
interface BaseClassRepository extends Neo4jRepository<Inheritance.BaseClass, Long> {
@@ -430,3 +434,42 @@ Here, the `literal` value has been used to match dynamically on a Label.
430
434
If you pass in `SomeLabel` as a parameter to the method, `MATCH (n:``SomeLabel``) RETURN n`
431
435
will be generated. Ticks have been added to correctly escape values. SDN won't do this
432
436
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.
0 commit comments