Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Data JPA: Query for Field in List of Objects

I'm looking for the correct method name to find all Documents with a given Target's type:

public class Document {

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

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

Unfortunately, all of the following method names aren't working:

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

So which is the correct method name to accomplish the desired feature?

like image 476
user3105453 Avatar asked Aug 15 '17 08:08

user3105453


1 Answers

You can try below query.

Using @Query annotation

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

Or

Using repository supported keywords

List<Document> findByTargetsType(String type);
like image 60
s7vr Avatar answered Nov 10 '22 06:11

s7vr