JPA: Find all articles that have a common set of tags

后端 未结 1 1134
渐次进展
渐次进展 2020-12-21 13:28

I have the following entities:

@Entity
public class Article{
    @Id
    private String id;

    @ManyToMany(cascade = CascadeType.PERSIST)
    private Set&l         


        
相关标签:
1条回答
  • 2020-12-21 13:56
    select a from Article a where :numberOfTags = 
        (select count(distinct tag.id) from Article a2
         inner join a2.tags tag
         where tag in :tags
         and a = a2)
    

    This basically counts the tags of the articles that are tags in the accepted set of tags, and if the number of such tags is eaqual to the size of the accepted set of tags (meaning that all tags in the set are tags of the article), returns the article.

    0 讨论(0)
提交回复
热议问题