Search code examples
neo4jspring-data-neo4j-4neo4j-ogm

Using Cypher to autogenerate IDs for SDN4


I have created a neo4j model with text file of cypher statements. Now i'd like to create an application that uses this database, and I'll be using SDN4. In my @NodeEntity class, I will need to have the GraphID of type Longdeclared. But this ID gets generated by SDN4, and my database doesn't have those IDs. Is there a way to have those created via Cypher so I can simply have my Java objects mapped correctly to the pre-existing nodes and relationships in the database?


Solution

  • The @GraphId field is always the internal node or relationship ID assigned by Neo4j and cannot be set to custom values. You're probably looking to assign your own ID value as a property of the node. This can be done by adding your own id field-

    @GraphId Long graphId; //required, this is the internal node/rel ID that must never be assigned by your code
    Long id; //your own primary key
    

    Now, when your database contains nodes with a property id assigned by the statement in your Cypher script, loading the entities via SDN will populate these id fields in your domain object.

    If you do not have your own primary keys but want an ID anyway, you can use a UUID plugin that will automatically assign UUID's to nodes when they're created via any means (SDN/Neo4j browser/API's etc.).