简体   繁体   中英

How to load the next depth level of referenced objects with neo4j-ogm

For getting familiar with neo4j-ogm I've created a very simple example with 3 persons, where Person A, knows Person B, which knows Person C and could verify this in the database browser.

Now I would like to load Person A with a depth-level of 1 (so the knows field is already filled with Person B) and when accessing the deeper nested fields (for example by traversing the data in a GUI) the referenced objects should be loaded. In this case, when checking the field 'knows' of Person B, the Person C should be loaded.

I'm loading Person A with the code below. But the Person B is not referenced at all and I haven't found any information about how to load the next level of data in general. At least it doesn't work by just accessing them (like in hibernate).

Result result = session.query("Match (p:Person {name: \"Person A\"}) return p", Collections.EMPTY_MAP);


@NodeEntity
public class Person {
    @Id @GeneratedValue
    private Long id;

    public String name;
    public Person knows;

    public Person() {
    }
    
    public Person(String name) {
        this.name = name;
    }
}

Your query Match (p:Person {name: \\"Person A\\"}) return p just returns the person in question, not any related person. You did not even model the relationship of type KNOWS (just referring here to the wording from your question).

The person should have an additional field:

@Relationship("KNOWS")
private List<Person> knownPeople;

And your query should look like Match (p:Person {name: \\"Person A\\"})-[r:KNOWS]-(op:Person) return p, collect(r), collect(op) to fetch the first level of relationships. You can of course add more patterns and return parts to your query to also get the next level etc. But in the end it might be easier to make use of session.loadAll(Person.class) and the sibling methods of the session API that generate the Cypher queries for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM