Search code examples
neo4jspring-data-neo4jneo4j-ogm

Neo4j-Spring-Data depth feature is not working with a rich relationship


I am using the API from Neo4j "Neo4j-OGM" and I get stuck trying to use the method findAll(int depth). My problem is when I have a rich relationship the depth gets lost. There are some situations it works properly like findAll(0), findById(int id, int depth), findAll(...) when the result is one, or when I have a simple relationship. I'll share my data structure and the jsons.

Person.class and Friend.class

@NodeEntity
@Data  
public class Person{
     @Id
     private String id;

     private String name;

     @Property("born")
     private int birthyear;

     @JsonIgnoreProperties({"person1"})
     @Relationship(type = "FRIEND_OF")
     @JsonInclude(JsonInclude.Include.NON_NULL)
     private List<Friend> friendOfs;

     @JsonIgnoreProperties({"person"})
     @Relationship(type = "FRIEND_OF",direction = Relationship.INCOMING)
     @JsonInclude(JsonInclude.Include.NON_NULL)
     private List<Friend> friendOfMy ;
}
@Data
@RelationshipEntity(value = "FRIEND_OF")
public class Friend {
     @Id
     @GeneratedValue
     private Long id;
     private String since;

     @StartNode
     @JsonIgnoreProperties({"friendOfs"})
     private Person person1;

     @EndNode
     @JsonIgnoreProperties({"friendOfMy"})
     private Person person;
}

When I make a get request to http://localhost:8080/rest/persons/all/2-> ("2"=depth)

[{
        {
    "id": "24",
    "name": "Tony",
    "birthyear": 0,
    "friendOfMy": [
        {
            "id": 0,
            "since": "2008-06-06",
            "person1": {
                "id": "2",
                "name": "Lina",
                "birthyear": 0,
                "friendOfMy": [
                    {
                        "id": 460,
                        "since": "2008-06-06",
                        "person1": {
                            "id": "1",
                            "name": "Fernando",
                            "birthyear": 0,
                            "friendOfMy": [
                                {
                                    "id": 462,
                                    "since": "2008-06-06",
                                    "person1": {
                                        "id": "4",
                                        "name": "Sui",
                                        "birthyear": 0
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        }
    ]},
     {
        "id": "3",
        "name": "Wlisses",
        "birthyear": 0,
        "friendOfMy": [
            {
                "id": 461,
                "since": "2000-06-06",
                "person1": {
                    "id": "1",
                    "name": "Fernando",
                    "birthyear": 0,
                    "friendOfMy": [
                        {
                            "id": 462,
                            "since": "2008-06-06",
                            "person1": {
                                "id": "4",
                                "name": "Sui",
                                "birthyear": 0
                            }
                        }
                    ]
                }
            }
        ]
    },
    .
    .
    .
    {
        "id": "1",
        "name": "Fernando",
        "birthyear": 0,
        "friendOfs": [
            {
                "id": 461,
                "since": "2000-06-06",
                "person": {
                    "id": "3",
                    "name": "Wlisses",
                    "birthyear": 0
                }
            },
            {
                "id": 460,
                "since": "2008-06-06",
                "person": {
                    "id": "2",
                    "name": "Lina",
                    "birthyear": 0,
                    "friendOfs": [
                        {
                            "id": 0,
                            "since": "2008-06-06",
                            "person": {
                                "id": "24",
                                "name": "Tony",
                                "birthyear": 0
                            }
                        }
                    ]
                }
            }
        ],
        "friendOfMy": [...]
    }]

As you can see the first result brings depth 3. My guess, it happened because of Java's memory reference.

I would like to know if it can be solved or if I am doing anything wrong.


Solution

  • No, you are not doing anything wrong here and your assumption is pretty right. Neo4j-OGM caches all entities that gets loaded during a transaction / session. In the case of a findAll it collects all entities that got loaded and assigns them to their relationships. This means if it finds Sui on the second level -as you have defined the depth- of Wlisses' friends it will attach it to Fernando in the result of Tony if it is a matching relationship.

    There is right now no mechanism in Neo4j-OGM to avoid this.