Spring Data JPA:查询对象列表中的字段

use*_*453 4 spring-data spring-data-jpa spring-data-mongodb

我正在寻找正确的方法名称,以找到所有Document具有给定Target的的type

public class Document {

    @Indexed(name = "targets")
    private List<Target> targets;

    public class Target {
        private String value;
        private String type;
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,以下所有方法名称均无效:

List<Document> findByTargetType(...);
List<Document> findByTargetsTargetType(...);
List<Document> findByTargetsContainsTargetType(...);
List<Document> findByTargetsContainsTargetTargetType(...);
List<Document> findByTargetsContainingTargetType(...);
List<Document> findByTargetsContainingTargetTargetType(...);
Run Code Online (Sandbox Code Playgroud)

那么,实现所需功能的正确方法名称是?

use*_*814 6

您可以尝试下面的查询。

使用@Query注释

@Query("{ 'targets.type' : ?0 }")
List<Document> findByTargetsType(String type);
Run Code Online (Sandbox Code Playgroud)

要么

使用存储库支持的关键字

List<Document> findByTargetsType(String type);
Run Code Online (Sandbox Code Playgroud)