我有以下Spring Data Neo4j 4.2.0.BUILD-SNAPSHOT实体:
@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
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);
}
我使用以下构造函数创建VoteGroup
:
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;
}
但是当我尝试使用以下内容找到之前保存的VoteGroup
时
VoteGroup voteGroup = getVoteGroupForDecisionOnCriterion(decision.getId(), criterion.getId());
我的voteGroup.decision
和voteGroup.criterion
为空..
但如果我在findOne
方法之后立即致电:
voteGroup = voteGroupRepository.findOne(voteGroup.getId());
voteGroup.decision
和voteGroup.criterion
已正确填充。
我的存储库方法/ Cypher有什么问题以及如何修复它?
答案 0 :(得分:1)
查询
@Query("MATCH (d:Decision)<-[:VOTED_FOR]-(vg:VoteGroup)-[:VOTED_ON]->(c:Criterion) WHERE id(d) = {decisionId} AND id(c) = {criterionId} RETURN vg")
只返回VoteGroup节点,因此,这就是OGM可以映射的全部内容。 如果您也想要决策和标准,那么您必须返回这些节点及其关系。这样的事情应该有效:
@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")
此博文包含更多示例:http://graphaware.com/neo4j/2016/04/06/mapping-query-entities-sdn.html
BTW你共享的VoteGroup构造函数不会被OGM使用,你需要一个no-args构造函数。