Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for uniqueness #36

Closed
rgan opened this issue Feb 14, 2012 · 10 comments
Closed

Support for uniqueness #36

rgan opened this issue Feb 14, 2012 · 10 comments

Comments

@rgan
Copy link

rgan commented Feb 14, 2012

Would like to add support for uniqueness using the UniqueFactory functionality in Neo4j 1.6. An approach is to add a Unique annotation to a field in a node entity which will create the node using the UniqueFactory in EntityStateHandler and NodeEntityState - as implemented in these 3 commits:

rgan@492929e
rgan@ccd230b
rgan@8fef84e

Do you see issues with this approach?

@jexp
Copy link
Contributor

jexp commented Feb 14, 2012

Hi Rajaram,

I'll have a look into your approach. Could you actually fork the project and issue a pull-request? That makes it easier to see everything in one place.

My idea was to add @indexed(type = UNIQUE) to properties but that would have some more widespread implications (e.g vetoing property changes) and might be too much complexity.

The other idea was just to have a small wrapper around UniqueFactory in Neo4jTemplate for creating unique entities (similar to createNodeAs())

@tibimunteanu
Copy link

I have a problem using @indexed(unique=true).
I have a User class with @indexed(unique=true) String uniqueId; and String name; and i save it.
Then if another user has refference to the first user's uniqueId in his list of friends, when the FRIEND relationship is created, the name property of the first user vanishes. (is they have many properties, they all vanish).
How do i fix this?

@jexp
Copy link
Contributor

jexp commented Apr 9, 2012

How did you acquire the first (unique) user when adding it to the second user as a friend? Can you share the code?

@tibimunteanu
Copy link

I am developing a facebook application and when i login i get the json with all my data from facebook, including friends [{id:1124332123, name:John Smith},{id:5632454536,name: Anna Molly}], so i only have refference to their id and name but if they allready exist in the graph, they have many other properties. so when i refference them i add a new User object with only the unique id from facebook in the Set friends; of my user

@tibimunteanu
Copy link

for(int k = 0; k < me.getJsonObject("friends").getJsonArray("data").length(); k++) {
String fbId = me.getJsonObject("friends").getJsonArray("data").getJsonObject(k).getString("id");
User user = new User(fbId);
this.friends.add(user);
}

this way, if one of them allready exists will be replaced by a node with only the fbid property.

@tibimunteanu
Copy link

and i can't mannually check for their existance because some users have thousands of friends and likes and other things in their jsons and it would take too long and would use many resources...i think

@jexp
Copy link
Contributor

jexp commented Apr 9, 2012

The problem is - as you save the new User and its other properties are null those will override the existing ones, so for those users it might be sensible to really use the template.getOrCreateNode()

And try to change your code so that it doesn't do the get's on the json all the time, store

me.getJsonObject("friends").getJsonArray("data")

in a variable.

@tibimunteanu
Copy link

Thank you for your reply!
I will use the getOrCreateNode(String index, String key, Object value, Map<String, Object> properties);
but the documentation on this is not very clear and i can't figure out the 4 parameters that this method takes.

  • String index - i think shuld be the name of the index, in my case "User" (since i didn't mannually set a name in the @indexed annotation)
  • String key - ?
  • Object value - the User friend object with the fbid set from the json?
  • Map<String,Object> properties - ?

and where should i use the getOrCreateNode(..)?

for(int k = 0; k < friends.length(); k++) {

String fbId = friends.getJsonObject(k).getString("id");

User friend = getOrCreateNode(....); //here?
this.friends.add(friend);

}

@jexp
Copy link
Contributor

jexp commented Apr 10, 2012

  • String index - i think shuld be the name of the index, in my case "User" (since i didn't mannually set a name in the @indexed annotation) -> correct
  • String key - ? -> uniqueId (field name)
  • Object value - the User friend object with the fbid set from the json? -> only the fbid-String
  • Map properties - ? the initial properties if the node is to be created

You might have to call template.postEntityCreation(node, User.class) after creating it.

@tibimunteanu
Copy link

Yep! That did the trick. Thank you very much for your help. I think you guys are doing a great job with sdn and neo4j!

@jexp jexp closed this as completed Aug 6, 2012
benday280412 pushed a commit to benday280412/spring-data-neo4j that referenced this issue Oct 30, 2012
… Added test to prove spatial geom nodes are now removed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants