Spring 数据 JPA:InvalidDataAccessApiUsageException:参数值 [web] 与预期类型不匹配 [java.util.Collection (n/a)];

Spring Data JPA: InvalidDataAccessApiUsageException: Parameter value [web] did not match expected type [java.util.Collection (n/a)];

我有一个包含字符串列表的实体。我想在该列表中搜索另一个列表,例如:我想要所有具有指定标签列表的主题。所以基本上,我希望我提供的字符串列表包含在每个实体拥有的字符串列表中。看起来很简单,但是我出错了。


主题实体:

public class Topic {
    // ...
    @ElementCollection
    @Column(name = "tags")
    private List<String> tags;
    // ...
}

存储库中的查询:

@Query("select t from Topic t where t.tags in (:tags)")
List<Topic> findAllByTags(@Param("tags") List<String> tags);

测试:

@Test
void findAllByTags() {
    List<String> tags = new ArrayList<>(Arrays.asList("web", "mobile"));

    List<Topic> allByTags = topicService.findAllByTags(tags);
}

异常:

org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [web] did not match expected type [java.util.Collection (n/a)]; nested exception is java.lang.IllegalArgumentException: Parameter value [web] did not match expected type [java.util.Collection (n/a)]

您必须加入标签:

@Query("select t from Topic t join t.tags tags where tags in (:tags)")
List<Topic> findAllByTags(@Param("tags") List<String> tags);