Spring data does a lot to help you focus on writing your cypher queries while it handles mapping the results for you. However, when your queries become more complex, it starts to struggle. This is where you need to step in and explicitly map the query results into your domain objects.
In this post, we will look at how you can query a path and map the results using Spring Data Neo4j.
Simple Queries Are Mapped for You
For simple queries, you can get away with only defining the relevant entity and relationship classes, Spring will do the rest.
Let’s use a
findAll
function (returns all nodes):
Calling the
findAll
function will correctly collect the cities from the database and map them for you.
Everything is good at this point.
Another example of setting up a Neo4j entity class can be found in the
Spring Data Neo4j - getting started documentation
.
Extracting Paths Requires Your Assistance
The code above is not suitable for a cypher query that returns a path.
The following query will be used for this section (taken from the
Apoc plugin’s documentation
):
This query returns the path between two cities and the total distance of that path.
Unfortunately, there is no way to represent this information using the entity and relationship classes defined earlier. Furthermore, there is no way to currently do this using Spring Data Neo4j full stop. It personally took me a long time to realise this fact, until I came across this excerpt in their
documentation
.
Custom queries do not support a custom depth. Additionally, @Query does not support mapping a path to domain entities, as such, a path should not be returned from a Cypher query. Instead, return nodes and relationships to have them mapped to domain entities.
Somehow I managed to keep brushing over this information when I went through the docs…
This means you cannot do something like this:
The
@QueryResult
annotation allows you to define a custom object to contain the results of your cypher query, more can be found in the
Spring docs
If you do this, it will still execute, but there will be no information about the cities filled in.
How to Manually Map a Path
To be able to map the path shown in the previous section, you need to manually assign the results from the cypher query into objects.
This can be done with the following code (large snippet incoming):
This iteration of
PathRepository
extends
SimpleNeo4jRepository
to inherit some of the more common queries without requiring you to implement them yourself. You do not
need
to extend this class, but I suggest you do.
I don’t think there is any need to explain the rest of the example, I tidied up the code as best I could and added comments for clarity. Hopefully, they are enough!
Conclusion
As I eventually found out and have now mentioned to you, at the time of writing this post, Spring Data Neo4j does not support the automatic mapping of queries containing paths. Therefore you will need to take your fate into your own hands and extract the data yourself. The code in the penultimate section (the long code snippet) shows you how to do this. Using a similar structure, you should be able to adapt this for your own use to solve your own problems. I hope this saves you some time as it took me a while to realise this was the only way to retrieve a path, when using Spring Data Neoj4 anyway…
If you enjoyed this post or found it helpful (or both) then please feel free to follow me on Twitter at
@LankyDanDev
and remember to share with anyone else who might find this useful!