Search code examples
neo4jneo4j-ogmneo4j-ogm-bolt-driver

loadAll not loading relationship nodes with different labels


In Java Eclipse I created my code based on the tutorial on neo4j OGM https://neo4j.com/docs/ogm-manual/current/tutorial/ but when I use the loadAll() method it loads all the nodes and the relatonships that are of the same label, but it does not load the nodes with different labels. These are the two Nodes I have created. This is a simple example of what I'm trying to do but not working.

@NodeEntity
public class Child {

    @Id@GeneratedValue
    private Long id;
    private String name;

    @Relationship(type = "PARENT_OF", direction  = Relationship.INCOMING)
    Parent parent;

    public String getName() {
        return name;
    }

    public Parent getParent() {
        return parent;
    }
}

@NodeEntity
public class Parent {

    @Id@GeneratedValue
    private Long id;
    private String name;

    @Relationship(type = "PARENT_OF")
    Set<Child> children;

    public String getName() {
        return name;
    }

    public Set<Child> getChildren() {
        return children;
    }
}

And this is how I am trying to access the data.

import org.neo4j.ogm.config.Configuration;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;

public class Main {
    private SessionFactory sessionFactory=null;
    private Session session=null;
    private Configuration configuration=null;
    private final String uri = "bolt://localhost:7687";
    private final String user = "neo4j";
    private final String password = "admin";

    private void connect(String entityName) {
        System.out.println("Connecting to Neo4j ("+uri+")");
        if(configuration==null) {
            configuration = new Configuration.Builder()
                .uri(uri)
                .credentials(user, password)
                .build();
        }
        sessionFactory = new SessionFactory(configuration, entityName);
        session = sessionFactory.openSession();
    }

    private void disconnect() {
        System.out.println("Closing the connection.");
        sessionFactory.close();
    }

    public Collection<Parent> getParents() {
        connect(Parent.class.getName());
        Collection<Parent> projects = session.loadAll(Parent.class);        
        disconnect();
        return projects;
    }

    public static void main(String[] args) {
        Main m = new Main();
        Collection<Parent> a = m.getParents();
        for (Iterator iterator = a.iterator(); iterator.hasNext();) {
            Parent parent = (Parent) iterator.next();
            System.out.println(parent.getName());
            System.out.println(parent.getChildren());           
        }
    }
 }

I alredy tried what was on this link https://github.com/neo4j/neo4j-ogm/issues/32. If anyone can tell me what I'm doing wrong I would apreciate it. I'm neo4j-ogm 3.1.1 and neo4j-ogm-bolt-driver3.0.2


Solution

  • Your SessionFactory configuration does only have the Parent class set as an entity to work with. Neo4j-OGM will only map nodes to classes it knows of.

    You should provide e.g. the package both classes are in when creating the SessionFactory.