【问题标题】:Help with rewriting a HQL query ( Hibernate Query Language )帮助重写 HQL 查询(休眠查询语言)
【发布时间】:2010-01-19 20:50:30
【问题描述】:

HQL 菜鸟在这里。如何在不使用存在子句的情况下重写此 HQL 查询。在 SQL 中,我可以只加入 VisitorProfileField 表和 Visitor 表,并使用 AND 添加存在的条件。

但在普通的 HQL 中,我猜我无法克服一些语法违规。非常感谢您的帮助。

"select distinct v from Visitor v where v.id not in (select o.id from Operator o) " +
      " and exists (select vpf from VisitorProfileField vpf " +
      " where vpf.visitor = v and vpf.profileField.name = :subscription_field " +
      " and vpf.numberVal = :type and vpf.stringVal = :manual) "

【问题讨论】:

    标签: java hibernate hql


    【解决方案1】:

    我不确定我是否明白你的查询的意思,但我猜是这样的:

    select distinct vpf.visitor 
    from VisitorProfileField vpf 
    where vpf.profileField.name = :subscription_field 
          and vpf.numberVal = :type and vpf.stringVal = :manual
          and vpf.visitor.id not in (select o.id from Operator o)
    

    【讨论】:

    • 意思是这样的:从访问者列表中选择那些不是运营商的人,以及订阅了邮件列表的人。 exists 子句有助于找到最后一部分。在您的解决方案中,我根本没有看到正在使用的访问者表。原始查询希望满足 VisitorProfileField 表中某些条件的访问者表中的人。
    • 我认为 vpf.visitor 给了我访客。在 HQL 中,您无需显式提及所有实体(即 SQL 中的表)即可使用它们,您只需浏览关系即可。因此,当我“选择不同的 vpf.visitor”时,我将通过满足条款的 VisitorProfileField 获得所有访问者。
    • 运营商是访客的子类吗?否则,“不在”似乎很危险。在 HQL 中,您还可以查询对象的(子)类。因此,如果 Operator 是访问者的子类,您实际上可以编写 'vpf.visitor.class != Operator'
    • 炒,非常感谢。我想上周当我看到这个解决方案时我太菜鸟了,它对我没有任何意义。现在我已经阅读了一些,这个解决方案似乎很完美。不过,我仍在考虑您对运营商的评论。
    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多