I found that a Node-entity with a natural key or an external id (generated by the application) annotated with @Id would not cause any trouble.
Meanwhile, an attempt to define an entity representing a relationship (annotated with @RelationshipProperties) without making use of internal id causes an exception (fragment):
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'neo4jTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'neo4jTemplate' parameter 1:
Error creating bean with name 'neo4jMappingContext' defined in class path resource [org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.class]:
Internally generated ids can only be assigned to one of [class java.lang.Long, interface org.springframework.data.neo4j.core.schema.ElementId, long]
Here's a minimal reproducible example:
@RelationshipProperties
@Getter
@Setter
public class FooRelationship {
@RelationshipId
private String naturalKey;
@TargetNode
private FooNode target;
}
In this example, the value of the naturalKey is always expected to be provided (hence there's no @GeneratedValue annotation).
If instead, this relationship would make use of the internal id:
@RelationshipProperties
@Getter
@Setter
public class FooRelationship {
@RelationshipId
@GeneratedValue
private Long id;
private String foo;
@TargetNode
private FooNode target;
}
there would be a warning suggesting using an externally generated id.
But with the following version specifying an externally generated id the application would fail to run:
@RelationshipProperties
@Getter
@Setter
public class FooRelationship {
@RelationshipId
@GeneratedValue(GeneratedValue.UUIDGenerator.class)
private String uuid;
@TargetNode
private FooNode target;
}
Exception (fragment):
Caused by: java.lang.IllegalStateException: The class
... .FooRelationship
for the properties of a relationship is missing a property for the generated, internal ID (@Id @GeneratedValue Long id
) which is needed for safely updating properties
SDN dependency:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>7.0.6</version>
<scope>compile</scope>
</dependency>
Am I missing something?