Merge two nodes on session.save (unique nodes)

877 views Asked by At

I'm trying to map some JSON objects to java objects and then save these objects to my neo4j db.

I have tried to use simple neo4j-ogm and run: session.save(object), but if some nodes already exist they are duplicated instead of being merged.

If I create a unique constraint on the value, then I get an exception when I try to run: session.save(object) if the nodes already exists.

I would like to know if there is a solution using neo4j-ogm, or i need to add Spring Data Neo4J (SDN) to resolve this problem?

1

There are 1 answers

0
Luanne On BEST ANSWER

As of Neo4j OGM 2.1.0, you can use @Index for this. Annotate your field with @Index(unique=true, primary=true) and session.save will use a MERGE instead of CREATE

See http://neo4j.com/docs/ogm-manual/current/reference/#reference_programming-model_indexing in the docs