Search code examples
spring-data-neo4jspring-data-neo4j-4

SDN 4 Collection of NodeEntity inside of RelationshipEntity


I'd like to create a following @RelationshipEntity:

@RelationshipEntity(type = "VOTE_GROUP")
public class VoteGroup{

    @GraphId
    private Long id;

    @StartNode
    private Decision decision;

    @EndNode
    private Criterion criterion;

    private Object value;

}

And @NodeEntity:

@NodeEntity
public class Vote extends Authorable {

    private final static String CONTAINS = "CONTAINS";

    private double weight;

    private String description;

}

Is it possible to add a collections of Vote to my VoteGroup relationship entity? something like this:

@RelationshipEntity(type = "VOTE_GROUP")
public class VoteGroup{

    @GraphId
    private Long id;

    @StartNode
    private Decision decision;

    @EndNode
    private Criterion criterion;

    private Object value;

    private Set<Vote> votes = new HashSet<>();

}

Will it work ? If so, will I be able to get this list of Votes later.. paginate them ?

The reason why I'm asking about this:

Right now in my current implementation I have a separated VoteGroup entity but the performance of Vote/VoteGroup creating is very slow so this is why I'm looking for a new approach how to fix it.

@NodeEntity
public class VoteGroup extends BaseEntity {

    private static final String VOTED_ON = "VOTED_ON";
    private final static String VOTED_FOR = "VOTED_FOR";
    private final static String CONTAINS = "CONTAINS";

    @Relationship(type = VOTED_FOR, direction = Relationship.OUTGOING)
    private Decision decision;

    @Relationship(type = VOTED_ON, direction = Relationship.OUTGOING)
    private Criterion criterion;

    @Relationship(type = CONTAINS, direction = Relationship.OUTGOING)
    private Set<Vote> votes = new HashSet<>();

    private double avgVotesWeight;

    private long totalVotesCount;

}

Solution

  • The foundation principles underlying Neo4j are: Nodes, Properties and Relationships.

    • Nodes can have properties and relationships.
    • Relationships can have properties.

    To add a collection of entities onto a relationship would be like a relationship having relationships. Not possible.

    You could probably fake it with OGM using a property and @Convert, however, perhaps it is best not to go against the grain of the system, and look for another way to model your domain.

    There may also be other approaches to addressing performance. Perhaps raise another issue that specifically describes the performance problems being faced.