我遇到了一个问题,无法想出解决方案。 所以我有这段代码:
public List<Post> getPosts(List<PostStrategy> allowedStrategyList, Set<Tag> allowedTags, int page, int resultsPerPage) {
return entityManager.createQuery("select post from Post post join post.tags tag where post.postStrategy in (:postStrategyList) and (:allowedTagsSize = 0 or tag in (:allowedTags))", Post.class)
.setParameter("postStrategyList", allowedStrategyList)
.setParameter("allowedTags", allowedTags)
.setParameter("allowedTagsSize", allowedTags.size())
.setFirstResult((page - 1) * resultsPerPage)
.setMaxResults(resultsPerPage)
.getResultList();
}
这段代码存在的问题在于,当有人使用多个标签进行搜索(例如:#video、#image),它会返回包含这两种标签和其中一种标签的帖子。
我希望它能够返回同时包含 #video 和 #image 标签的帖子。为了实现这一点,我需要检查列表是否包含另一个列表的所有元素。
我搜索了一段时间,并尝试了不同的方法,但是目前还没有找到解决方法。
我曾尝试用“post.tags in (:allowedTags)”替换“tag in (:allowedTags)”,但是这会导致 SQL 无效。