3

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

Luanne
  • 19,145
  • 1
  • 39
  • 51
Marc Byfield
  • 510
  • 4
  • 14

2 Answers2

6

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.

Luanne
  • 19,145
  • 1
  • 39
  • 51
  • Here are the related issues to this problem: [DATAGRAPH-782](https://jira.spring.io/browse/DATAGRAPH-782), [DATAGRAPH-771](https://jira.spring.io/browse/DATAGRAPH-771) and [DATAGRAPH-700](https://jira.spring.io/browse/DATAGRAPH-700). Also take a look at [this article](http://graphaware.com/neo4j/2016/04/06/mapping-query-entities-sdn.html) about the use of custom queries. – Gilberto Torrezan Jun 06 '16 at 19:25
  • "In a future release" - is this feature still in the future 2 years later? An entity w/o relationships is almost useless. – Abhijit Sarkar Feb 05 '18 at 22:57
  • SDN 4 already maps entities returned in custom queries to domain entities. – Luanne Feb 06 '18 at 04:24
  • 4
    I'm using SDN 5 and it does not return relationships. Do I need to do something special? – grandouassou May 09 '18 at 13:53
1

In the meantime, it seems to work for depth 1. The result nodes contain a non-null reference to relationship objects. But it's important that you return all 3 results: Node (n), relationship type (r), relationship node (u), e. g.:

@Query("MATCH(n:MyEntity) MATCH (n)-[r:MY_REL_TYPE]-(u) RETURN n, r, u LIMIT {0}")
List<MyEntity> getAll(int limit);
PAX
  • 1,056
  • 15
  • 33