2016-02-09 82 views
0

我試圖執行一個查詢返回一個路徑,但是,儘管在neo4j Web UI執行的查詢返回正確的結果,neo4j-ogm返回null。我已經從Maven安裝了neo4j-ogm-api,core:2.0.0-M01Neo4j OGM 2.0查詢路徑

我的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 
    } 
} 

請向我解釋我做錯了什麼?

回答

1

不支持返回完整路徑。相反,你需要返回要映射到域實體的節點和關係,如:

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是什麼 - 只有SDN支持QueryResult。

+0

啊,謝謝。關於QueryResult,在測試時,我使用了Object qrm = session.query ... :)))我希望遲早neo4j-ogm將支持與py2neo相同的功能。 – Vahagn