cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Cycling relationship within same entity

Hi everyone,

I'm new to neo4j, I have one question hoping someone more experienced could help.
So I need to model one to many relationship within User type, the use case is that a user can be lead to another user, so on my UserEntity I want to have "lead" attribute which will be lead of user if he has it, and also "mentees" attribute which will be list of users which that user leads if he leads some.

Here below you can see how I modelled my UserEntity

@Node("User")
public class UserEntity {

    @Id
    private String userName;

    @Property("password")
    private String password;

    @Relationship(type = "IS_LEAD_BY", direction = Relationship.Direction.OUTGOING)
    private UserEntity lead;

    @Relationship(type = "IS_LEAD_BY", direction = Relationship.Direction.INCOMING)
    private Set<UserEntity> mentees;

And down below you can see the response I get when fetching user:

{
        "userName": "user1",
        "password": "pass1",
        "lead": {
            "userName": "user2",
            "password": "pass2",
            "lead": null,
            "mentees": [
                {
                    "userName": "user1",
                    "password": "pass1",
                    "lead": {
                        "userName": "user2",
                        "password": "pass2",
                        "lead": null,
                        "mentees": [
                            {
                                "userName": "user1",
                                "password": "pass1",
                                "lead": {
                                    "userName": "user2",
                                    "password": "pass2",
                                    "lead": null,
                                    "mentees": [...

Basically the problem is in response I get endless loop, because user 1 has lead user2, which then again has user1 as mentee and it goes on like this.. Is it somehow possible to limit depth for the attribute or something like that? Is there a better way to model entity?

Hope someone answers my question, looking forward to it.

Have a great day

3 REPLIES 3

I am not an expert on Spring Data Neo4j, but I played with it and decided it wasn't for me due to the behavior you are experiencing. Like you, I have a hierarchical set of similar nodes. My use case is to process one node within the tree and its children at a time. Spring Data Neo4j wanted to load and update the entire graph regardless where I was in the tree. I decided to write my own repository using cypher statements to fetch and update just the portions I needed. I wrote neo4j procedures, which I call in cypher, to calculate the metrics on need on any portion of the graph.

The following is from the Spring Data Neo4j manual (section 6.1.6 Connecting Nodes). It seems to imply the behavior you are getting is expected, when you have nodes with bidirectional relationships like you have (like I did as well).

https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#mapping

I think I will just write my own implementation then for this and not rely on SDN, I somehow skipped this part in documentation you linked, so good to know it is expected behaviour.
Thanks for the reply, it was very helpful!

Just for completion: There are projection (and multi-level projections) for defining what to fetch (and starting with 6.2 what to persist) Projections documentation
Also what you are facing in your returned JSON data is not a Spring Data Neo4j problem but a missing definition where to cut the infinite loop sth. like @JsonIgnore or @JsonIgnoreProperties for the relationship defining side.