Search code examples
springneo4jspring-data-neo4jneo4j-ogmspring-data-neo4j-5

Are ids in @NodeEntity objects unique (ie not recycled) in SDN (Spring Data Neo4j) 5.0.2.RELEASE?


As per the example code retrieved here: http://projects.spring.io/spring-data-neo4j/

A node entity can be created with the following code:

@NodeEntity
public class Movie {

  @Id @GeneratedValue Long id;
  String title;

  Person director;

  @Relationship(type="ACTS_IN", direction = Relationship.INCOMING)
  Set<Person> actors;

  @Relationship(type = "RATED")
  List<Rating> ratings;
}

Note the @Id and @GeneratedValue annotations on the id attribute.

As I understand it, the @Id specifies the attribute id as the primary key, and the @GenerateValue causes this value to be generated on creation (defaulting to an incremental id generation).

In earlier versions of SDN, it was recommended not to use the internal Neo4j ids since they were an offset and may, therefore, be recycled.

My question is, with SDN 5.0.2.RELEASE, is it confirmed that using @Id @GeneratedValue now guarantees that the id will be unique and not recycled?

Thanks


Solution

  • Neo4j now provides the org.neo4j.ogm.id.UuidStrategy class for use as an optional argument to the @GeneratedValue annotation. Since UuidStrategy returns a generated UUID string, this causes the annotated variable to contain a UUID (instead of the recyclable Long native ID generated by neo4j, which is the default).

    The org.neo4j.ogm.domain.annotations.ids.ValidAnnotations unit test has several examples of how to use UuidStrategy, for both nodes and relationships. (It also shows the use of a custom IdStrategy, should you want to write your own.)