0

I have a graph similar to this:

(t:Teacher)-[:TEACHES]-(s:Student)-[:ATTENDS]-(s:Subject)-[:REQUIRES]-(b:Book)

expressed in @NodeEntity classes.

I need to load all @NodeEntities for teachers with Ids withing a given range, and fetch all students taught by them, with their Subjects and the Books required by each Subject, all in one query.

I tried

int depth=3;
List<Long> ids = newArrayList(3,4,5);
session.loadAll(Teacher.class, ids, depth);

I actually started with the corresponding GraphRepository findAll it is the same thing.

Unfortunately I get a list of all available Teachers in the database, because each student happens to be taught by all teachers. I also tried running a custom @Query though it appears that you cannot specify custom depth How to control depth on custom Spring Data Neo4j repository methods? nor return more than one node from custom cypher query.

Do you have an idea why my approach is wrong? This seams to be a simple use of neo4j and I'm stuck with fetching my whole graph.

Community
  • 1
  • 1
dziadeusz
  • 21
  • 3
  • I am using the spring data neo4j 4.0.0.RELEASE. I would like to avoid upgrading to 4.2.0.M1 version of spring data neo4j (would it help?) as it would require also upgrading my spring boot version and other dependencies. – dziadeusz Nov 04 '16 at 08:16

1 Answers1

0

Yes, when you call session.loadAll with your model to depth 3 you are basically returning all teachers as there is no pattern match applied to the load query to restrict it to the nodes you are talking about. So thinking about this from the perspective of depth isn't really going to help what you are trying to do.

There are two (albeit similar) ways to get this working.

a) You correctly mention using a custom @Query. This is the easiest way and you don't need to specify depth. In order for the query to work you must also return your relationships. Your signature would be something like this:

    @Query("MATCH (t:Teacher)-[teacherRels:TEACHES]-(st:Student)-[studentRels:ATTENDS]-(su:Subject)-[subjectRels:REQUIRES]-(b:Book) WHERE ID(t) in {ids} RETURN t, teacherRels, st, studentRels, su, subjectRels, b")
    public Iterable<Teacher> findTeachersByIds(@Param("ids") List<Long> ids);

b) You can do the same thing as above with session.query instead (which is what the above method will basically call).

If you do want to upgrade you should use 4.2.0.RC1 which should be out on 28/11/2016 (You can use 4.2.0.BUILD-SNAPSHOT in the mean time if you want. It's more stable than 4.2.0.M1).

digx1
  • 1,100
  • 5
  • 9