2014-05-24 46 views
3

我有我的第一次去與框架和我的Java很生鏽。我堅持通過框架將信息寫入數據庫。我一直在關注文檔並擁有一個Person界面。Tinkerpop幀寫入數據庫 - 新手

public interface Person { 
    @Property("name") 
    public String getName(); 

    @Adjacency(label="knows") 
    public Iterable<Person> getKnowsPeople(); 

    @Adjacency(label="knows") 
    public void addKnowsPerson(final Person person); 

    @GremlinGroovy("it.out('knows').out('knows').dedup") //Make sure you use the GremlinGroovy module! #1 
    public Iterable<Person> getFriendsOfAFriend() 
} 

這是從文檔中提取的。 我可以使用這個簡單的代碼從圖中獲取數據。

TinkerGraph graph = TinkerGraphFactory.createTinkerGraph(); //This graph is pre-populated. 
FramedGraphFactory factory = new FramedGraphFactory(new GremlinGroovyModule()); //(1) Factories should be reused for performance and memory conservation. 

FramedGraph framedGraph = factory.create(graph); //Frame the graph. 

Person person = framedGraph.getVertex(1, Person.class); 
person.getName(); // equals "marko" 

我想知道的是我將如何創建一個新的Person對象並將其寫入圖形。因爲人只有一個接口,我不能做的:

Person person2 = new Person(); 
person2.setName("John"); 
person2.setAge(36); 
framedGraph.addVertex(person2); 

所以我已經試過它實現了人,並添加以下代碼

PersonImpl johnBoy = new PersonImpl(); 
johnBoy.setName("John"); 
johnBoy.setAge(36); 
johnBoy.addKnowsPerson(person); 
person.addKnowsPerson(johnBoy); 

但是我得到以下空指針一類PersonImpl的而我現在真的陷入困境。我希望有人能夠幫助我。

Exception in thread "main" java.lang.NullPointerException 
    at com.tinkerpop.blueprints.impls.tg.TinkerGraph.addEdge(TinkerGraph.java:331) 
    at com.tinkerpop.frames.FramedGraph.addEdge(FramedGraph.java:310) 
    at com.tinkerpop.frames.annotations.AdjacencyAnnotationHandler.addEdges(AdjacencyAnnotationHandler.java:87) 
    at com.tinkerpop.frames.annotations.AdjacencyAnnotationHandler.processVertex(AdjacencyAnnotationHandler.java:53) 
    at com.tinkerpop.frames.annotations.AdjacencyAnnotationHandler.processElement(AdjacencyAnnotationHandler.java:26) 
    at com.tinkerpop.frames.annotations.AdjacencyAnnotationHandler.processElement(AdjacencyAnnotationHandler.java:15) 
    at com.tinkerpop.frames.FramedElement.invoke(FramedElement.java:89) 
    at com.sun.proxy.$Proxy4.addKnowsPerson(Unknown Source) 
    at com.elecrticdataland.utility.TinkerTest.main(TinkerTest.java:45) 

與許多感謝,

約翰

回答

1

無法除非代理的方式創建一個Person。換句話說,您不能使用該接口的具體實現,它必須由FramesGraph動態構建。

你的代碼來創建這裏Person

FramedGraph framedGraph = factory.create(graph); //Frame the graph. 

Person person = framedGraph.getVertex(1, Person.class); 
person.getName(); // equals "marko" 

沒有這個,創建Person實施不會知道底層的東西,並注射給factory.create()

+0

非常感謝斯蒂芬Graph實例。我是否正確思考,因此無法通過接口向圖中添加頂點,並且您需要使用這種類型的代碼來實現:Graph graph = new Neo4jGraph(「/ tmp/my_graph」); Vertex a = graph.addVertex(null); Vertex b = graph.addVertex(null); a.setProperty(「name」,「marko」); b.setProperty(「name」,「peter」); Edge e = graph.addEdge(null,a,b,「knows」); e.setProperty(「since」,2006); graph.shutdown(); – user3672265

+1

我沒有完全跟着你。如果你想添加一個新的Person,那麼做:'Person p = g.addVertex(null,Person.class);' –

+0

非常感謝,Stephen。 – user3672265