Neo4j OGM 2.0 查询路径
Neo4j OGM 2.0 query path
我正在尝试执行 returns 路径中的查询,但是,尽管在 neo4j Web UI
returns 中执行的相同查询得到了正确的结果,但 neo4j-ogm
returns null
。我已经从 Maven 安装了 neo4j-ogm-api,core:2.0.0-M01
。
我的 java 代码如下所示:
Root.java:
@NodeEntity
public class Root
{
@GraphId
public Long id;
@Relationship(type = "Branch", direction = Relationship.OOUTGOING)
public List<Branch> branches = new ArrayList<>();
public Branch addLeaf(Leaf leaf, float length)
{
Branch b = new Branch(this, leaf);
b.length = length;
leaf.branch = b;
branches.add(b);
return b;
}
}
Leaf.java:
@NodeEntity
public class Leaf
{
@GraphId
public Long id;
@Property
public String color;
@Relationship(type = "Branch", direction = Relationship.INCOMING)
public Branch branch;
}
Branch.java:
@RelationshipEntity
public class Branch
{
@GraphId
public Long id;
public Branch(Root root, Leaf leaf)
{
this.root = root;
this.leaf = leaf;
}
@Property
public float length;
@StartNode
public Root root;
@EndNode
public Leaf leaf;
}
然后,为了测试让我们做
public class Main {
public static void main(String[] args) {
SessionFactory sessionFactory = new SessionFactory("com.my.package.name");
Session session = sessionFactory.openSession();
Root r = new Root()
r.addLeaf(new Leaf(), 1);
r.addLeaf(new Leaf(), 2);
session.save(r);
//Until this point everything is alright and
// all 3 nodes and 2 relationships are created
String query = "MATCH path = (l1:Leaf)-[*1..100]-(l2:Leaf) WITH path LIMIT 1 RETURN path";
QueryResultModel qrm = session.query(query, new HashMap<String, Object>());
// qrm.result.get(0).get("path") is null
}
}
请向我解释我做错了什么?
不支持返回完整路径。相反,您需要 return 将要映射回域实体的节点和关系,例如:
MATCH path = (l1:Leaf)-[*1..100]-(l2:Leaf) WITH path,l1 LIMIT 1 RETURN l1,nodes(path),rels(path)
这会给你一个 org.neo4j.ogm.session.Result
对象。如果您从底层 Map 中检索 l1,您应该有一个完全水化的 Leaf 实体。
顺便说一句,不确定 QueryResultModel
是什么 - QueryResult 仅在 SDN 中受支持。
我正在尝试执行 returns 路径中的查询,但是,尽管在 neo4j Web UI
returns 中执行的相同查询得到了正确的结果,但 neo4j-ogm
returns null
。我已经从 Maven 安装了 neo4j-ogm-api,core:2.0.0-M01
。
我的 java 代码如下所示:
Root.java:
@NodeEntity
public class Root
{
@GraphId
public Long id;
@Relationship(type = "Branch", direction = Relationship.OOUTGOING)
public List<Branch> branches = new ArrayList<>();
public Branch addLeaf(Leaf leaf, float length)
{
Branch b = new Branch(this, leaf);
b.length = length;
leaf.branch = b;
branches.add(b);
return b;
}
}
Leaf.java:
@NodeEntity
public class Leaf
{
@GraphId
public Long id;
@Property
public String color;
@Relationship(type = "Branch", direction = Relationship.INCOMING)
public Branch branch;
}
Branch.java:
@RelationshipEntity
public class Branch
{
@GraphId
public Long id;
public Branch(Root root, Leaf leaf)
{
this.root = root;
this.leaf = leaf;
}
@Property
public float length;
@StartNode
public Root root;
@EndNode
public Leaf leaf;
}
然后,为了测试让我们做
public class Main {
public static void main(String[] args) {
SessionFactory sessionFactory = new SessionFactory("com.my.package.name");
Session session = sessionFactory.openSession();
Root r = new Root()
r.addLeaf(new Leaf(), 1);
r.addLeaf(new Leaf(), 2);
session.save(r);
//Until this point everything is alright and
// all 3 nodes and 2 relationships are created
String query = "MATCH path = (l1:Leaf)-[*1..100]-(l2:Leaf) WITH path LIMIT 1 RETURN path";
QueryResultModel qrm = session.query(query, new HashMap<String, Object>());
// qrm.result.get(0).get("path") is null
}
}
请向我解释我做错了什么?
不支持返回完整路径。相反,您需要 return 将要映射回域实体的节点和关系,例如:
MATCH path = (l1:Leaf)-[*1..100]-(l2:Leaf) WITH path,l1 LIMIT 1 RETURN l1,nodes(path),rels(path)
这会给你一个 org.neo4j.ogm.session.Result
对象。如果您从底层 Map 中检索 l1,您应该有一个完全水化的 Leaf 实体。
顺便说一句,不确定 QueryResultModel
是什么 - QueryResult 仅在 SDN 中受支持。