0

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?

user3777228
  • 159
  • 1
  • 14

1 Answers1

2

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
Community
  • 1
  • 1
Sumeet Sharma
  • 2,573
  • 1
  • 12
  • 24
  • Thanks a lot. So using MERGE is the only way to achieve this or for Spring-data-neo4j there is some alternate solution. – user3777228 Jun 26 '14 at 14:59
  • check out that thread attached in the answer.. the guy has tried several ways to achieve it but i prefer creating using merge.. – Sumeet Sharma Jun 26 '14 at 15:25
  • To create or update nodes using MERGE,do you use Query annotation in SDN or Core JAVA API.I have this doubt as I read that the core JAVA API is better in some cases compared to SDN. I have created a SDN Project and use repositories to interact with the DB. Will it be better to use Neo4jTemplate instead of repositories.Thanks in advance. – user3777228 Jul 14 '14 at 19:37