I have a following Spring Data Neo4j 4.2.0.BUILD-SNAPSHOT entities:
@NodeEntity
public class VoteGroup extends BaseEntity {
private static final String VOTED_ON = "VOTED_ON";
private final static String VOTED_FOR = "VOTED_FOR";
@Relationship(type = VOTED_FOR, direction = Relationship.OUTGOING)
private Decision decision;
@Relationship(type = VOTED_ON, direction = Relationship.OUTGOING)
private Criterion criterion;
...
}
@NodeEntity
public class Decision extends Commentable {
@Relationship(type = VOTED_FOR, direction = Relationship.INCOMING)
private Set<VoteGroup> voteGroups = new HashSet<>();
...
}
@NodeEntity
public class Criterion extends Authorable {
@Relationship(type = VOTED_ON, direction = Relationship.INCOMING)
private Set<VoteGroup> voteGroups = new HashSet<>();
....
}
repository:
@Repository
public interface VoteGroupRepository extends GraphRepository<VoteGroup> {
@Query("MATCH (d:Decision)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} RETURN vg")
VoteGroup getVoteGroupForDecisionOnCriterion(@Param("decisionId") Long decisionId, @Param("criterionId") Long criterionId);
}
I'm creating VoteGroup
with a following constructor:
public VoteGroup(Decision decision, Criterion criterion, double avgVotesWeight, long totalVotesCount) {
this.decision = decision;
decision.addVoteGroup(this);
this.criterion = criterion;
criterion.addVoteGroup(this);
this.avgVotesWeight = avgVotesWeight;
this.totalVotesCount = totalVotesCount;
}
but when I try to find previously saved VoteGroup
with:
VoteGroup voteGroup = getVoteGroupForDecisionOnCriterion(decision.getId(), criterion.getId());
my voteGroup.decision
and voteGroup.criterion
are NULL..
but if I call right after that findOne
method:
voteGroup = voteGroupRepository.findOne(voteGroup.getId());
voteGroup.decision
and voteGroup.criterion
are populated properly.
What is wrong with my repository method/Cypher and how to fix it ?
The query
@Query("MATCH (d:Decision)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} RETURN vg")
returns only the VoteGroup node and so, this is all that the OGM can map. If you want the decision and criterion too, then you have to return those nodes and their relationships. Something like this should work:
@Query("MATCH (d:Decision)<-[for:VOTED_FOR]-(vg:VoteGroup)-[on:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} RETURN vg,d,for,on,c")
This blog post contains more examples: http://graphaware.com/neo4j/2016/04/06/mapping-query-entities-sdn.html
BTW the VoteGroup constructor you shared will not be used by the OGM, you need a no-args constructor.