2017-04-06 54 views
2

我正在嘗試使用Neo4j Java螺栓驅動程序創建具有許多屬性的節點。我的代碼目前看起來是這樣的:Neo4j Java螺栓驅動程序:創建具有許多屬性的節點

String statement =               
     " CREATE (p:Person {id: {id}, firstName: {firstName}, lastName: {lastName}, gender: {gender}, birthday: {birthday}, creationDate: {creationDate}, locationIp: {locationIp}, browserUsed: {browserUsed}, speaks: {speaks}, emails: {emails}})"; 
String parameters = parameters(           
     "id", String.valueOf(operation.personId()),       
     "firstName", operation.personFirstName(),        
     "lastName", operation.personLastName(),        
     "gender", operation.gender(),           
     "birthday", operation.birthday().getTime(),       
     "creationDate", operation.creationDate().getTime(),     
     "locationIP", operation.locationIp(),         
     "browserUsed", operation.browserUsed(),        
     "speaks", operation.languages(),          
     "emails", operation.emails()); 

    try (Session session = driver.session(AccessMode.WRITE)) {     
    try (Transaction tx = session.beginTransaction()) {      
     StatementResult result = tx.run(statement, params);     
     tx.success();               
     tx.close();               
    }                  
    } 

但使用HTTP和JSON,它可以簡化聲明:

String statement =               
     " CREATE (p:Person {props})"; 

,併發送一個JSON對象,這是這樣的:

{props: 
    {id: bla, 
    firstName: bla, 
    lastName: bla, 
    ... 
    } 
} 

Neo4j Bolt驅動程序API中是否有方法使用CREATE語句的後一版本並將參數作爲地圖提供?

回答

0

獨立於API。該CREATE (p:Person {props})語法被棄用,取而代之的:

CREATE (p:Person) SET p = {props}

,或者如果你想成爲添加劑

CREATE (p:Person) SET p += {props}

您還可以設置多個一次。

看到:https://neo4j.com/docs/developer-manual/current/cypher/clauses/create/#create-create-node-with-a-parameter-for-the-properties

與你一起發送的地圖螺栓驅動程序。或Values.parameters("id", id, "firstName", firstname, ...)

+0

所以你說如果'String statement =「CREATE(p:Person)SET p = {props}」;'和'Value params = Values.parameters(「id」,id,「firstName」 ,firstname,...)'then'tx.run(statement,params)'將創建一個具有所有這些屬性的Person節點? –

相關問題