在Java Eclipse中,我根据neo4j OGM https://neo4j.com/docs/ogm-manual/current/tutorial/上的教程创建了代码,但是当我使用loadAll()方法时,它会加载具有相同标签的所有节点和关系,但不会加载用不同的标签加载节点。这是我创建的两个节点。这是我要尝试但不起作用的简单示例。
@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;
}
}
这就是我试图访问数据的方式。
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());
}
}
}
我确实尝试过此链接https://github.com/neo4j/neo4j-ogm/issues/32上的内容。如果有人能告诉我我在做什么错,我会很感激。 我是neo4j-ogm 3.1.1和neo4j-ogm-bolt-driver3.0.2
答案 0 :(得分:1)
您的SessionFactory
配置仅将Parent
类设置为可使用的实体。 Neo4j-OGM只会将节点映射到它知道的类。
您应提供例如创建SessionFactory
时,两个类都位于该程序包中。