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

Spring Data JPA: Query for Field in List of Objects

我正在寻找正确的方法名称来查找具有给定 Target 的所有 Documenttype:

public class Document {

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

    public class Target {
        private String value;
        private String type;
    }
}

很遗憾,以下所有方法名称均无效:

List<Document> findByTargetType(...);
List<Document> findByTargetsTargetType(...);
List<Document> findByTargetsContainsTargetType(...);
List<Document> findByTargetsContainsTargetTargetType(...);
List<Document> findByTargetsContainingTargetType(...);
List<Document> findByTargetsContainingTargetTargetType(...);

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

您可以尝试以下查询。

使用@Query注释

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

或者

使用存储库支持的关键字

List<Document> findByTargetsType(String type);