Problem Statement
Let’s say we have the following data in Neo4j
/java-platform/java/java---projects--and--code-examples/java---non-native-libraries/java---spring-family/java---spring-data/java---spring-data---spring-data-modules-(builtin)/java---spring---spring-data-neo4j-(sdn)/spring-data-neo4j-(sdn)-5---custom-complex-@query-auto-map-pojo-objects/sdn5-complex-query-example.png)
We want to return a person node along with:
- all the movies that person DIRECTED
- all the props for each DIRECTED movie
- SHOULD NOT return any movies that person ACTED_IN
Failed Solution (using Depth feature)
the simple solution is utilizing the depth feature
@Repository
public interface PersonRepository extends Neo4jRepository<Person, String> {
Optional<Person> findByUuid(String uuid, @Depth int depth);
}
This query FAILS because it also returns movies the person ACTED_IN :[
Solution (without using Depth)
@Repository
public interface PersonRepository extends Neo4jRepository<Person, String> {
@Query("MATCH (p:Person {uuid: $uuid})-[d:DIRECTED]->(m:Movie)-[h:HAS]->(prop:Prop)" +
" return p, collect(d), collect(hh), collect(m), collect(prop)")
Optional<Person> getPersonWithDirectedMovies(String uuid);
}