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

SpringDataNeo4j-4 derived finders with custom queries


I am currently using SpringDataNeo4j–4.1.0-BUILD-SNAPSHOT. I access the database using the “extends GraphRepository" structure and the Neo4jTemplate. Among other things I am using simple derived finder queries like:

Event findById (Long id) 

This works fine and returns results as expected. Now I have cases where I want to find an entry by property but only retrieve certain relationships:

@Query("MATCH (n:Event{dbId:{0}})-[r:species|figure]->(m) RETURN n,r,m")
Event findById (Long id)

I know this is right now not supported. Is there a possible workaround for this behaviour without having to construct the object on my own and will this be implemented in the future versions of SDN since OGM 2.0.0-M01 now offers the possibility perform queries like this?


Solution

  • You can use Neo4jTemplate.query and retrieve "n" from the Result.

    n will be an Event that's been hydrated with properties and relationships for species and 'figure`.

    Or, you can instead create a @QueryResult that the repository method annotated with the custom Query returns, and you'll still get back the hydrated Event.

    This functionality is already available in the SDN 4.1 snapshot (4.1.0.BUILD-SNAPSHOT)

    Your query above @Query("MATCH (n:Event{dbId:{0}})-[r:species|figure]->(m) RETURN Return n,r,m") Event findById (Long id) is not supported because there are three entities returned by the query, but only a single one expected to be returned.