Search code examples
javaspring-bootneo4jneo4j-ogm

Why neo4j-ogm not saves relations to embedded neo4j-database?


I try to get a working system of an embedded tomcat-server with spring-boot, joinfaces and the embedded neo4j-graph-database with the object-mapping ogm. Everything seems to work fine. I commit my sources to https://svn.riouxsvn.com/circlead-embedd/circlead-embedded/

The problem is that all neo4j-ogm-examples (see i.e. http://www.hascode.com/2016/07/object-graph-mapping-by-example-with-neo4j-ogm-and-java/) show that @Relationship works with ogm. But when i test it with

@NodeEntity
public abstract class GenericNode<T> implements INode<T> {

    @GraphId
    public Long id;

    @SuppressWarnings("unused")
    private void setId(Long id) {
        this.id = id;
    }

    public String label;

    @Relationship(type = "PARENT_OF", direction = Relationship.INCOMING)
    public Set<T> parents = new HashSet<T>();

    @Relationship(type = "CHILD_OF", direction = Relationship.OUTGOING)
    public Set<T> children = new HashSet<T>();

    ...

then all relations seem not to be written in the database, because the lines

    Role rp = new Role("Role 1");
    Role rc = new Role("Role 2");
    rc.addParent(rp);
    session.save(rc);
    Iterable<Role> roles = session.query(Role.class, "MATCH (x) RETURN x;", Collections.<String, Object>emptyMap());

    for (Role role : roles) {
        System.out.println(role);
    }

show in the console that the relations of the database are missing. It seems that only in the active session-relations are found. After a server-reboot all relations are missing.

Role [id=52, label=Role 2, parents=[]]
Role [id=53, label=Role 1, parents=[]]
Role [id=54, label=Role 1, parents=[]]
Role [id=55, label=Role 2, parents=[54]]

I have no clue what occurs this kind of error. I use neo4j-ogm 2.1.2 and neo4j 3.1.3.

Any idea?


Solution

  • The result you are seeing is expected - neo4j-ogm maps what you return in your cypher query (plus what you already have in your session). If you also want related entities then return the relationship and other nodes:

    MATCH (x)-[r]-(x2) RETURN x,r,x2
    

    or if you want e.g. only parent field hydrated:

    MATCH (x)<-[r:PARENT_OF]-(p) RETURN x,r,p
    

    This will hydrate first level only. To hydrate all levels (parent of a parent) you need to use variable length path and return its nodes and relationships (returning paths directly doesn't work reliably):

    MATCH p=(x)-[:PARENT_OF*..]-() RETURN nodes(p),rels(p)