参考Spring Data Mongodb中的子列表查询方法而不使用@Query或过滤父初始结果

问题描述 投票:0回答:1

我所要求的可能是不可能的,因为所有类似的示例似乎都使用 @Query 或在父字段上进行初始查找,然后在子列表上进行流/过滤/收集。

EventChange 包含订阅列表,如下所示:

public class Subscription{
    private String id;
    private String subscribeDn;
    private DocumentType documentType;
    private Object document;
}

public class EventChange{
    private String id;
    private String eventDn;
    private Action action;
    private DocumentType documentType;
    private List<Subscription> subscriptions;
}

对于 EventChange 存储库,我的任务是向用户 (userDn) 返回他们未发起但已订阅的所有订阅和事件。所以基本上我会寻找 eventDn != userDn 且 documentType == Subscription.DocumentType 以及 userDn 至少在列表之一中找到的事件

这是我通过 QueryMethod 技术所能得到的。

public List<EventChange> findByDnAndDocumentTypeAndActionAnd??????(String userDn, DocumentType documentType, Action action, ????);

问号显然是占位符,我认为如果可能的话,相关值将放在其中。我在封闭系统上工作,因此所提供的代码是手动输入的以说明场景。

预先感谢您的时间和精力!

java mongodb spring-data
1个回答
0
投票

我认为 JPA 查询方法没有一个简单的解决方案,但我认为您可以通过编写自定义 SQL 查询来摆脱困境。

参见:https://docs.spring.io/spring-data/jpa/reference/jpa/query-methods.html#jpa.query-methods.at-query

@Query("select e from EventChange e where ...")
List<EventChange> findByDnAndDocumentTypeAndCustom(String userDn, DocumentType documentType, Action action);

然后将

...
替换为您想要的逻辑,然后您可以通过拥有自己的 SQL 子查询来获得更细粒度。

© www.soinside.com 2019 - 2024. All rights reserved.