07-27-2023, 12:45 PM
I'd like to ensure I do not create multiple nodes with my neo4j database. I see [here][1] that this is the preferred method (even after reviewing this [page][2]), but I do not understand what the code is really doing in that example. Can someone possibly elaborate for the layman/noob?
Literal code is:
> Create a Cypher execution engine and a unique constraint.
try ( Transaction tx = graphdb.beginTx() )
{
graphdb.schema()
.constraintFor( DynamicLabel.label( "User" ) )
.assertPropertyIsUnique( "name" )
.create();
tx.success();
}
return new ExecutionEngine( graphdb() );
> Use MERGE to create a unique node.
Node result = null;
ResourceIterator<Node> resultIterator = null;
try ( Transaction tx = graphDb.beginTx() )
{
String queryString = "MERGE (n:User {name: {name}}) RETURN n";
Map<String, Object> parameters = new HashMap<>();
parameters.put( "name", username );
resultIterator = engine.execute( queryString, parameters ).columnAs( "n" );
result = resultIterator.next();
tx.success();
return result;
}
[1]:
Literal code is:
> Create a Cypher execution engine and a unique constraint.
try ( Transaction tx = graphdb.beginTx() )
{
graphdb.schema()
.constraintFor( DynamicLabel.label( "User" ) )
.assertPropertyIsUnique( "name" )
.create();
tx.success();
}
return new ExecutionEngine( graphdb() );
> Use MERGE to create a unique node.
Node result = null;
ResourceIterator<Node> resultIterator = null;
try ( Transaction tx = graphDb.beginTx() )
{
String queryString = "MERGE (n:User {name: {name}}) RETURN n";
Map<String, Object> parameters = new HashMap<>();
parameters.put( "name", username );
resultIterator = engine.execute( queryString, parameters ).columnAs( "n" );
result = resultIterator.next();
tx.success();
return result;
}
[1]:
[To see links please register here]
[2]:[To see links please register here]