I have an entity which has a list of strings. I want to search in that list with another list, for example: I want all topics with the specified list of tags. So basically, I want that my list of strings that I provide to be included in the list of strings that every entity has. Seems pretty simple, but I gen an error.
Topic entity:
public class Topic {
// ...
@ElementCollection
@Column(name = "tags")
private List<String> tags;
// ...
}
Query in repository:
@Query("select t from Topic t where t.tags in (:tags)")
List<Topic> findAllByTags(@Param("tags") List<String> tags);
Test:
@Test
void findAllByTags() {
List<String> tags = new ArrayList<>(Arrays.asList("web", "mobile"));
List<Topic> allByTags = topicService.findAllByTags(tags);
}
Exception:
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)]
CodePudding user response:
You have to join the tags:
@Query("select t from Topic t join t.tags tags where tags in (:tags)")
List<Topic> findAllByTags(@Param("tags") List<String> tags);