Reputation: 979
With SDN Neo4j, is it possible to create a node only if the node with a matching field does not exist? So for instance, I have a an existing node lets say labelled "Road" with value "N4". Now I need to create a new node with a relationship to this node lets say "drives on". So I do this just fine. However the second time around I need to check if a Node with label "Road" already exists with value "N4". If it does then make the new node point to the existing one not create a new Road node with value N4?
I am trying to do the equivalent using SDN I think:
MATCH (root { name: 'root' })
CREATE UNIQUE (root)-[:X]-(leaf { name:'D' })
RETURN leaf
No node connected with the root node has the name D, and so a new node is created to match the pattern.
Can this be done using SDN domain modelling at the point of insert somehow?
thanks
Upvotes: 0
Views: 190
Reputation: 41676
In general it might not so a good idea to create roads on demand??
Actually it is pretty simple
class Road {
@Indexed(unique=true) String name;
}
class Person {
@Indexed(unique=true) String name;
Road drivesOn;
}
Road r = template.save(new Road("N4")); // does a merge if the road already exists, but of course more expensive
Person p = new Person("Timmy")
p.setDrivesOn(r);
// also merges person if it doesn't exist,
// the default is that there is only one relationship ever between this person and _a_ road.
template.save(p);
Upvotes: 1
Reputation: 5001
Why don't you use a custom repository class? You can just re-use your cypher query.
public interface LeafRepository extends GraphRepository<Leaf> {
@Query("MATCH (r:{0}) CREATE UNIQUE (r)-[:X]-(leaf { name:'D' }) RETURN leaf")
Leaf createLeaf(Road road);
}
It's been a while since I've used SDN though, but the main idea should be clear. You'll have to use SDN 3 if you want to use Neo4j 2.0. Read the repository tutorial for more information.
Upvotes: 0