具有自定义查询和复合实体的Neo4j SDN4存储库方法

具有自定义查询和复合实体的Neo4j SDN4存储库方法,neo4j,cypher,spring-data-neo4j-4,neo4j-ogm,Neo4j,Cypher,Spring Data Neo4j 4,Neo4j Ogm,我拥有以下SDN 4实体: @NodeEntity public class Decision { @Relationship(type = CONTAINS, direction = Relationship.INCOMING) private Set<Decision> parentDecisions; ... } 现在,如果存在parentD,则会出现以下异常: java.lang.RuntimeException: Result not of expe

我拥有以下SDN 4实体:

@NodeEntity
public class Decision {


    @Relationship(type = CONTAINS, direction = Relationship.INCOMING)
    private Set<Decision> parentDecisions;

...
}
现在,如果存在
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存储库方法和自定义密码查询正确实现此逻辑

已更新

我测试了frant.hartm所建议的方法。不幸的是,它仍然不起作用

例如,以下SDN4存储库方法正确返回2
决策:

@Query("MATCH (d:Decision)-[drd:FOLLOWS]->(following:Decision) WHERE d.id = {decisionId} RETURN following")
List<Decision> test(@Param("decisionId") Long decisionId);
仅返回main
decision
holder.getDecision()
),但返回
followD
holder.getDecision().getfollowdescisions()
)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;
    }

....

}
@NodeEntity
公共阶级决策{
私有静态最终字符串FOLLOWS=“FOLLOWS”;
@关系(类型=跟随,方向=关系。传入)
私人决定;
@关系(类型=跟随,方向=关系。传入)
公共集getFollowDecisions(){
返回后续决策;
}
@关系(类型=跟随,方向=关系。传入)
公共无效setFollowDecisions(集合followDecisions){
this.followDecisions=followDecisions;
}
....
}

我做错了什么?

核心问题是,您的查询返回多个
决策
实例(在不同的变量下-
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);
@QueryResult
公共类决策者{
决定d;
}
匹配(d:Decision),其中d.id={decisionId}“+

“可选匹配(d)核心问题是,您的查询返回多个
决策
实例(在不同的变量下-
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);
@QueryResult
公共类决策者{
决定d;
}
匹配(d:Decision),其中d.id={decisionId}“+

“可选匹配(d)我从未使用过SDN4,但我相信问题在于方法
getById
期望返回1行,而您返回3行,因为传递的
{decisionId}
有3个父对象。这样,查询将返回
d
(1个节点)和
parentD
(3个节点)之间的笛卡尔乘积。3x1=返回的3行。一种可能的解决方法是将自定义查询更改为
MATCH(d:Decision),其中d.id={decisionId}可选匹配(d),谢谢您的评论。不幸的是,无法处理相同的错误我从未使用过SDN4,但我相信问题在于方法
getById
期望返回1行,而您返回3行,因为传递的
{decisionId}
有3个父对象。这样,查询将返回
d
(1个节点)和
parentD
(3个节点)之间的笛卡尔乘积。3x1=返回的3行。一种可能的解决方法是将自定义查询更改为
MATCH(d:Decision),其中d.id={decisionId}可选匹配(d),谢谢您的评论。不幸的是,它不适用于相同的错误谢谢你的回复,但不幸的是它不适用。请查看我更新的问题以了解详细信息。请检查以查看它是否如我所述工作。一定还有别的。您能够创建/共享测试用例吗?感谢您的回复,但不幸的是,它不起作用。请查看我更新的问题以了解详细信息。请检查以查看它是否如我所述工作。一定还有别的。您是否能够创建/共享测试用例?
@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);