Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement composite keys in Neo4J

I need to ensure that a combination of more than one property values in all nodes is unique. How to do that in Neo4J.

From Neo4J documentation available at http://docs.neo4j.org/chunked/milestone/transactions-unique-nodes.html, it's possible to ensure uniqueness of one property. But what about combination of 2 or more.

like image 423
Amresh Avatar asked Feb 06 '13 10:02

Amresh


1 Answers

You could try

  public Node getOrCreateUserWithUniqueFactory(final String firstName, final String lastname, GraphDatabaseService graphDb) {
    UniqueFactory<Node> factory = new UniqueFactory.UniqueNodeFactory(graphDb, "users") {
      @Override
      protected void initialize(Node created, Map<String, Object> properties) {
        created.setProperty("id", properties.get("id"));

        created.setProperty("firstName", firstName);
        created.setProperty("lastName", lastname);
      }
    };

    return factory.getOrCreate("id", firstName + "_" + lastname);
  }
like image 138
Werner Kvalem Vesterås Avatar answered Oct 24 '22 20:10

Werner Kvalem Vesterås