Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Java Bolt Driver: Creating node with many properties

Tags:

java

neo4j

cypher

I'm trying to create a node that has many properties using the Neo4j Java Bolt driver. My code currently looks like this:

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();                                                           
    }                                                                       
  }

But using HTTP and JSON, it's possible to simplify the statement to:

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

And send a JSON object that's something like:

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

Is there a way in the Neo4j Bolt driver API to use this latter version of the CREATE statement and supply parameters as a map?

like image 244
Jonathan Ellithorpe Avatar asked Jun 21 '26 12:06

Jonathan Ellithorpe


1 Answers

That's independent of the API. The CREATE (p:Person {props}) syntax was deprecated in favour of:

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

or if you want to be additive

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

You can also set multiple at once.

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

With the bolt driver you sent a map along. or Values.parameters("id", id, "firstName", firstname, ...)

like image 193
Michael Hunger Avatar answered Jun 24 '26 02:06

Michael Hunger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!