title: How to create unique node in neo4j?
body: <p>I'd like to ensure I do not create multiple nodes with my neo4j database.  I see <a href="http://docs.neo4j.org/chunked/milestone/tutorials-java-embedded-unique-nodes.html#tutorials-java-embedded-unique-get-or-create" rel="nofollow noreferrer">here</a> that this is the preferred method (even after reviewing this <a href="https://stackoverflow.com/questions/13021636/unique-value-in-neo4j-nodes">page</a>), but I do not understand what the code is really doing in that example.  Can someone possibly elaborate for the layman/noob?</p>
  
  <p>Literal code is:</p>
  
  <blockquote>
    <p>Create a Cypher execution engine and a unique constraint.</p>
  </blockquote>
  
  <pre><code>try ( Transaction tx = graphdb.beginTx() )
  {
      graphdb.schema()
              .constraintFor( DynamicLabel.label( "User" ) )
              .assertPropertyIsUnique( "name" )
              .create();
      tx.success();
  }
  
  return new ExecutionEngine( graphdb() );
  </code></pre>
  
  <blockquote>
    <p>Use MERGE to create a unique node.</p>
  </blockquote>
  
  <pre><code>Node result = null;
  ResourceIterator&lt;Node&gt; resultIterator = null;
  try ( Transaction tx = graphDb.beginTx() )
  {
      String queryString = "MERGE (n:User {name: {name}}) RETURN n";
      Map&lt;String, Object&gt; parameters = new HashMap&lt;&gt;();
      parameters.put( "name", username );
      resultIterator = engine.execute( queryString, parameters ).columnAs( "n" );
      result = resultIterator.next();
      tx.success();
      return result;
  }
  </code></pre>