【问题标题】:How to check is ALL elements of a collection exists in another collection in HQL如何检查一个集合的所有元素是否存在于 HQL 的另一个集合中
【发布时间】:2012-12-12 15:42:21
【问题描述】:

我有一个具有技能实体映射集合的课程实体。现在我想通过 HQL 检查 course.skills 集合的所有元素是否存在于另一个集合中,我将在查询中作为参数传递。

我可以使用 IN 语句检查一个元素是否已经在另一个集合中,但我似乎无法弄清楚如何检查是否所有元素都存在。

希望有人可以帮助我。谢谢!

【问题讨论】:

  • 你能给出一些你尝试过的代码示例吗......因为问题不是很清楚

标签: hibernate collections mapping hql


【解决方案1】:

我很想为这个问题找到一个更优雅的解决方案,但我用这样的查询来解决它:

select course from Course course
where not exists (
    select skill.id from Skill skill
    where skill.id in :setOfSkillIdsToHave
    and skill.id not in (
        select courseSkill.id
        from Course course2
        inner join course2.skills courseSkill
        where course2.id = course.id))

或者,

select course from Course course
where :numberOfSkillsToHave = (
    select count(skill.id)
    from Course course2
    inner join course2.skills courseSkill
    where courseSkill.id in :setOfSkillIdsToHave
    and course2.id = course.id)       

【讨论】:

    猜你喜欢
    • 2011-05-23
    • 1970-01-01
    • 2021-04-05
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 2015-03-22
    • 2021-12-13
    • 2012-03-31
    相关资源
    最近更新 更多