5

Using the new Neo4j 2.3 OGM. When trying to load entities by id I have the following problem:

@NodeEntity
class Person {
    Long id;
    String name;

    @Relationship(type="Friend", direction = Direction.OUTGOING)
    public List<Person> friends;
}

assuming (1, "Alex") is friends with (2, "Joseph") and (3, "Guy"). (4, "Nati") is friends with (5, "Amit"), using the following code:

session.loadAll(Person.class, Arrays.toList(new Long() { 1L, 4L }), 1)

should return 2 Person objects, Alex containing two friends (Guy, Joseph) and Nati containing one friend yet what it actually returns is 5 objects (Alex, Guy, Joseph, Nati, Amit). Although Mike and Nati do contain their friends within, it seems weird (and certainly unwanted) that I requested Persons by two ids and got an Iterable containing 5. Does anyone know why this is? is this a bug?

Nachshon Schwartz
  • 15,289
  • 20
  • 59
  • 98

2 Answers2

1

This is by design. The OGM has a concept of search depth. By default (and in your example, explicitly) the search depth is 1, meaning fetch the requested objects from the graph along with their immediate neighbours. You can set the search depth explicitly if you don't want this behaviour. Setting it to zero like this:

session.loadAll(Person.class, Arrays.toList(new Long() { 1L, 4L }), 0)

will fetch just the requested objects.

Vince
  • 2,181
  • 13
  • 16
  • I think u didn't understant what happens, it returns Alex (containing Joseph and Guy) and Nati (containing Amit) until here alls good, but what's weird is it also returns 3 more Person objects (Amit, Joseph and Gut) in the Iterable. on the other hand if Person had a relationship to an object of type Job it would not load this into the iterable but only return it below the appropriate Person object. Just because the child object is of the same type of the parent it adds it to the Iterable – Nachshon Schwartz Nov 02 '15 at 20:05
  • `session.loadAll(Person.class, Arrays.toList(new Long() { 1L, 4L }), 0)` would not populate the friends relationship on the Alex and Nati objects – Nachshon Schwartz Nov 02 '15 at 20:07
  • 1
    Sorry Nayish, I misunderstood what you were saying. Yes, I would expect five objects to created, but the Iterable I would expect to contain only the two objects requested by id. So this looks like a bug and I've raised a ticket for it here : https://github.com/neo4j/neo4j-ogm/issues/79 – Vince Nov 02 '15 at 20:44
  • Thanks, i'd love if you looked at this other question too: http://stackoverflow.com/questions/33486334/load-an-object-in-neo4j-2-3-ogm-depth-of-2-very-slow – Nachshon Schwartz Nov 02 '15 at 20:46
1

This issue is now fixed in 1.1.4-SNAPSHOT build.

Vince
  • 2,181
  • 13
  • 16