Find entity by exact matching in collection
Use HAVING
with CASE
to count matched userId
and check equal with searched userIds
count.
@Query(value = "SELECT c FROM Conversation c LEFT JOIN c.usersIds cu GROUP BY c "
+ "HAVING SUM(CASE WHEN cu IN (:userIds) THEN 1 ELSE -1 END) = :userIdsCount")
List<Conversation> findByUserIds(@Param("userIds") List<String> userIds,
@Param("userIdsCount") Integer userIdsCount);