Reputation: 159
I am new to Neo4j and SDN . I was trying to create a primary key for my entities using @Indexed(unique=true). It creates unique nodes but will replace the other properties if i used the same property value which is indexed. Eg:
Class abc{
@Indexed(unique=true)
Long abc_id;
String abc_name;
}
new abc(1,"asd")// creates node with id: 1 name : asd
new abc(1,"xyz")// replaces asd with xyz .
However I want to throw an exception about primary key violation/duplicate node.
Is there some method to achieve the same?
Upvotes: 0
Views: 1260
Reputation: 2583
Currently the @Indexed implementation doesnt throw any exception for any duplicates as quoted in this JIRA ticket on the spring data neo4j forum : here and in this thread
You may create unique nodes running cypher queries (which will be faster). You can use MERGE to create or update nodes.
From the documentation
MERGE either matches existing nodes and binds them, or it creates new data and binds that. It’s like a combination of MATCH and CREATE that additionally allows you to specify what happens if the data was matched or created.
So you can do something like below if you want to update any node or create a node if that pattern doesnt exist.
MERGE (keanu:Person { name:'Keanu Reeves' })
ON CREATE SET keanu.created = timestamp()
ON MATCH SET keanu.lastSeen = timestamp()
RETURN keanu
Upvotes: 2