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

654 Views Asked by At

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

2

There are 2 best solutions below

1
cybersam On BEST ANSWER

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.)

1
meistermeier On

The statement is still valid that ids get reused.

Do not rely on this ID for long running applications. Neo4j will reuse deleted node ID’s. It is recommended users come up with their own unique identifier for their domain objects (or use a UUID).

from OGM documentation

It is basically a bad idea to have references to internal technical ids within an application.