Selecting where an entity contains a list thats a subset of another list

后端 未结 3 542
星月不相逢
星月不相逢 2020-12-05 20:19

I am writing a JPQL query and i have the following scenario. I have a Question entity which contains a list of Tags. I would like to select all Questions that contains a gi

3条回答
  •  星月不相逢
    2020-12-05 20:52

    Nayans solution does'nt work for me. Its selecting every 'x' which matches the first (or any?) entry of the given collection ':tags'. If this really worked for you, you should test you application again ;) might be JPA dependend - I don't know.

    Tip: Try Krzysztofs solution or use mine:

    SELECT x FROM Question x 
    WHERE x IN (
        SELECT y FROM Question y
        INNER JOIN y.tags yt
        WHERE yt IN (
            :tags
        )
        GROUP BY y
        HAVING COUNT( DISTINCT yt) = (
            :tagsSize // should be clear ;)
        )
    )
    

提交回复
热议问题