Search code examples
javaspringgroovyspring-data-neo4j-4neo4j-ogm

Custom query in Spring Data Neo4j not retrieving relationships


So for a few complex operations, I am using custom Cypher queries using the @Query annotation on my custom finder methods in my graph repositories. However, while it retrieves the node, it does not retrieve its direct relationships (i.e. only 1 level).

@Query("match (node:T) return node Order by node.requestedAt Desc LIMIT 100")
List<T> last100T();

@Query("match (node:T) where node.status = \"REQUESTED\" and  timestamp() - node.requestedAt >= 60000 return node")
List<Transit> findUnmatchedAndExpiredT();

I am using them like this - (code is in groovy):

 def nodes = TRepository.findUnmatchedAndExpiredT()
    nodes.forEach({
        node ->
            node.status = TStatus.DECLINED
            node.neighbourA.status = NeighbourAStatus.BARRED
            def neighbourBQueue = client.queue(node.neighbourB.username)
            neighbourBQueue.push(mapper.writeValueAsString(node))

    TRepository.save(node)
})

They are related like so:

    @NodeEntity
class T{

    public T(){
    }

    @GraphId
    Long id

    @Relationship(type = "REQUESTED_BY", direction = Relationship.OUTGOING)
    NeighbourB neighbourB

    @Relationship(type = "SERVICED_BY", direction = Relationship.OUTGOING)
    NeighbourA neighbourA
}

Both Neighbours A and B are null when the relationships do exist. What do? I'm using Spring boot 1.2.7.RELEASE with spring-data-neo4j:4.0.0.RELEASE


Solution

  • Custom queries (@Query) do not support a depth parameter and they map exactly what the query returns. If you're returning a single node, it'll map that single node. The query is not modified at runtime to include extra relationships.

    You can return the node ID instead and then load it with the default depth (1), or a custom depth.

    In a future release, SDN 4 will be able to map multiple entities returned in custom queries to domain entities.