2

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

Coder Shark
  • 437
  • 2
  • 5
  • 13

2 Answers2

2

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

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • Ah, thank you for this! Part of what prompted me to ask this question initially is because I did see something about a the UuidStrategy for generating the uuid, and therefore had wondered if the default strategy for @GenerateValue was also somehow managed and incremented. Using the UuidStrategy looks like the right option for me here. Cheers! – Coder Shark Jan 24 '18 at 06:05
1

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.

meistermeier
  • 7,942
  • 2
  • 36
  • 45
  • I see that the default strategy for @GeneratedValue is "InternalIdStrategy" from the reference. Very useful thank you. – Coder Shark Jan 24 '18 at 06:09