Search code examples
neo4jspring-data-neo4j

Spring Data Neo4j implement relationship entity with @RelationshipProperties and @TargetNode


I have the following legacy relationship entity which I'd like to upgrade to the latest SDN with @RelationshipProperties and @TargetNode:

@RelationshipEntity(type = "HAS_VALUE_ON")
public class RelationshipValue {

    @Id
    @GeneratedValue
    private Long graphId;

    @StartNode
    private Decision decision;

    @EndNode
    private Characteristic characteristic;

Could you please show how to implement it with @RelationshipProperties and @TargetNode annotations ?


Solution

  • A relationship with properties does not point towards both entities anymore but is a directed relationship. We do not make any assumptions if the TargetNode is an end or a start node. This is defined in the relationship-defining class.

    Assuming that RelationshipValue is used in Decision and it should get connected to Characteristic, you would define something like:

    @RelationshipProperties
    public class RelationshipValue {
    
        @Id
        @GeneratedValue
        private Long graphId;
    
        @TargetNode
        private Characteristic characteristic;
    

    and in Decision:

    public class Decision {
    
        @Relationship("HAS_VALUE_ON") // direction can be OUTGOING (default) or INCOMING
        private RelationshipValue relationshipValue;
    }
    

    x-post: https://community.neo4j.com/t/spring-data-neo4j-implement-relationship-entity-with-relationshipproperties-and-targetnode/38429/2?u=gerrit.meier