Warm tip: This article is reproduced from serverfault.com, please click

Spring Data / Neo4j @Query annotation property substitution

发布于 2020-12-01 06:07:48

I'm working on a Spring Data / Neo4j-based recommender service and ran into an issue with the @Query annotation. I'm trying to pass a property (the network_userid) into a Cypher query:

package io.woolford.neo4j.repository;

import io.woolford.neo4j.entity.PageUrl;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;


public interface RecommendationsRepository extends Neo4jRepository<PageUrl, Long> {
    @Query("MATCH (n {id: '$network_userid' }) RETURN n")     // simplified query for brevity
    List<PageUrl> getRecommendations(@Param("network_userid") String network_userid);
}

I thought it'd be as simple as putting a dollar-sign in front of the property name to substitute the property name with the property value (as above). I expected this:

MATCH (n {id: '1447e32a-3381-4e61-a8ae-9a05b8df4ddb'}) RETURN n

When I switched on debugging, it looks like the Cypher query hasn't replaced the property with the value:

[nio-8080-exec-1] o.n.o.drivers.bolt.request.BoltRequest   : Request: MATCH (n {id: '$network_userid' }) RETURN n with params {0=1447e32a-3381-4e61-a8ae-9a05b8df4ddb, network_userid=1447e32a-3381-4e61-a8ae-9a05b8df4ddb}

For good measure, I did a packet capture to see what's being sent across the wire:

...d'...............+MATCH (n {id: '$network_userid' }) RETURN n..0.$1447e32a-3381-4e61-a8ae-9a05b8df4ddb.network_userid.$1447e32a-3381-4e61-a8ae-9a05b8df4ddb......?..n.....

... which, again, suggests that the property substitution isn't happening.

Can you see what I'm doing wrong?

Source code: https://github.com/alexwoolford/snowplow-neo4j-recommender

Questioner
Alex Woolford
Viewed
0
meistermeier 2020-12-05 03:22:34

The only problem with your query is that you are escaping the '$network_userid' like a string but you should do: @Query("MATCH (n {id: $network_userid }) RETURN n") without the string literal indication.