我有以下SDN 4实体:
@NodeEntity
public class Decision {
@Relationship(type = CONTAINS, direction = Relationship.INCOMING)
private Set<Decision> parentDecisions;
...
}
我想通过ID找到此实体,并使用以下SDN4存储库方法返回其所有parentDecisions
和自定义密码查询:
MATCH (d:Decision) WHERE d.id = {decisionId} OPTIONAL MATCH (d)<-[rdp:CONTAINS]-(parentD:Decision) RETURN d, rdp, parentD
Decision getById(@Param("decisionId") Long decisionId);
现在,在现有parentD
的情况下,它失败了以下例外:
java.lang.RuntimeException: Result not of expected size. Expected 1 row but found 3
at org.neo4j.ogm.session.delegates.ExecuteQueriesDelegate.queryForObject(ExecuteQueriesDelegate.java:73)
at org.neo4j.ogm.session.Neo4jSession.queryForObject(Neo4jSession.java:382)
at sun.reflect.GeneratedMethodAccessor111.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
如何使用SDN4存储库方法和自定义Cypher查询正确实现此逻辑?
更新
我测试了 frant.hartm 建议的方法。不幸的是它仍然无法使用。
例如,以下SDN4存储库方法正确返回2 Decision
:
@Query("MATCH (d:Decision)-[drd:FOLLOWS]->(following:Decision) WHERE d.id = {decisionId} RETURN following")
List<Decision> test(@Param("decisionId") Long decisionId);
但下一个:
@Query("MATCH (d:Decision) WHERE d.id = {decisionId} OPTIONAL MATCH (d)-[rdf:FOLLOWS]->(followD:Decision) RETURN d as decision, collect(rdf), collect(followD) ")
DecisionHolder test1(@Param("decisionId") Long decisionId);
仅返回MAIN decision
(holder.getDecision()
),但带有followD
(holder.getDecision().getFollowDecisions()
)决策的空收集。
这是Decision
实体:
@NodeEntity
public class Decision {
private static final String FOLLOWS = "FOLLOWS";
@Relationship(type = FOLLOWS, direction = Relationship.INCOMING)
private Set<Decision> followDecisions;
@Relationship(type = FOLLOWS, direction = Relationship.INCOMING)
public Set<Decision> getFollowDecisions() {
return followDecisions;
}
@Relationship(type = FOLLOWS, direction = Relationship.INCOMING)
public void setFollowDecisions(Set<Decision> followDecisions) {
this.followDecisions = followDecisions;
}
....
}
我在做什么错?
核心问题是您的查询返回多个Decision
实例(在不同的变量下-d
和parentD
),因此它不能只是任意选择一个实例。使用@QueryResult
提取所需的值。也使用COLLECT
获得单个结果。
@QueryResult
public class DecisionHolder {
Decision d;
}
MATCH (d:Decision) WHERE d.id = {decisionId} " +
"OPTIONAL MATCH (d)<-[rdp:CONTAINS]-(parentD:Decision) " +
"RETURN d, collect(rdp), collect(parentD)")
DecisionHolder getById(@Param("decisionId") Long decisionId);
更新:
对于正确映射正确关系实体和与同一类型的简单关系的关系,不得混合。
如果您具有与相同类型的简单关系和关系实体
当他们表示相同的关系类型使用关系实体类中的关系定义时(例如
Set<ParentDecisionRelationship> parentDecisions
)当它们代表不同类型时,重命名其中一种