0
votes

I have a node object that looks like this:

@NodeEntity
public class Title {
    private Long id;
    private String name;
    public void setName(String name){
        this.name=name;
    }
}

The name is the natural identifier for this object. I want to ensure that one Node is created per value for name. What is the recommended way to do this with Spring Data Neo4j? I tried adding this logic to the setter like so:

public void setName(String name){
    this.name=name;
    this.id = new Long(name.hashCode());
}

But when I do this, the nodes are not getting created at all. I'm using Neo4jTemplate to save this object:

Title t = new Title();
t.setName("blah");
neo4jTemplate.save(t);
2
It sounds like as of SDN 4.1.1, there is no support for indexes. My workaround for this issue at the moment is to create an index for 'name' and then manipulate the object graph I'm trying to save by modifying the title to an equivalent one that already exists in the database(if one exists). Extremely cumbersome - if there are any other ways around this, I'd appreciate help.Navin Viswanath

2 Answers

1
votes

There is no merge functionality in SDN 4.x. You can do either of the following-

  1. Set up a unique constraint (which you should have anyway), and then deal with a ConstraintViolatedException

OR

  1. Load the entity by name and use it if it exists, or create a new one if it does not.
1
votes

The id is provided by neo4j, you can not assign one yourself. The SDN(more specifically, the OGM) does not have an API to deal with database administration; you must do that yourself. I am not sure i completely understand what you are trying to achieve, but it is a good idea to set a unique constraint on the title field. See http://neo4j.com/docs/stable/query-constraints.html#query-constraints-unique-nodes.