I am developing a Facebook app in which I login with my Facebook account and Facebook returns me a JSON with my Facebook data as a JSON which I project into a User.class (me) like so:
@NodeEntity public class User{ @GraphId Long NodeId; String facebook_id; String facebook_name; }
and then I do a template.save(me);
this creates a node in the graph with 2 properties: Example: facebook_id="1000023453464"
and name="John Smith"
and the nodeId is set automatically by neo4j: Example nodeId:1
.
The problem is that when I login again, it creates another node with same properties and nodeId:2.
I avoided this by checking if the user already exists in the graph before I saved it in the graph like so:
User retrieved_user = userRepository.findByFacebook_id(me.getFacebook_id());
if(retrieved_user != null) { me.setNodeId(retrieved_user.getNodeId()); }
Now, whenever I login, it doesn't create a duplicate node, it just updates the properties on the existing node.
But this is not what I'm looking for because when I import other data such as list of friends which can be about 1000 nodes to create, I don't want my app to check each individual node for his existence in the database before it saves it.
My point is: If there is a way in which I can manually set the nodeId before I template.save(me)
the problem would be solved and my app would not have to check if the node exists in the graph. So if this is doable please help me achieve it. I tried for example the setHighId(215431323)
, but I get exceptions. I tried to @Override
the IdGenerator, EntityIdGenerator, etc. but no success.
If there is another way in which I can save users without having to check if they exist and avoid duplicate nodes, please let me know.
I don't think there is a way to manually set the id. But even if there was, you still had to check if your node exists. Checking for the existence of a node is not that expensive anyway, it's just an index check, so it should be O(log n).