我有以下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
已正确填充。
我的存储库方法/密码有什么问题以及如何解决它?
查询
@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
顺便说一句,您共享的 VoteGroup 构造函数不会被 OGM 使用,您需要一个无参数构造函数。