【问题标题】:HQL Query fails when using join and with clause使用 join 和 with 子句时 HQL 查询失败
【发布时间】:2014-04-20 00:05:38
【问题描述】:

晚上好,

我正在使用 Grails,我正在尝试执行 HQL 查询

我有一个对象 Opportunity,在它里面有一个对象 Entity,在 Entity 里面有一个 Titles 集合。每个 Title 对象可以是 main 或不是 main(main 是一个布尔字段,显示哪些标题是默认标题)。所以我正在做的查询是这样的:

从 Opportunity 中选择机会作为机会加入机会。entity.titles 作为 entityTitle,entityTitle.isMain 为 true

但此查询失败并显示以下消息:

org.hibernate.hql.ast.InvalidWithClauseException: with-clause 表达式没有引用与 with-clause 关联的 from-clause 元素。

我已尝试添加实体和标题表,但仍然失败。如果我删除 with 子句,它可以正常工作,但我需要过滤标题。

谢谢。

【问题讨论】:

    标签: grails hql grails-orm


    【解决方案1】:

    试试这个查询:

    select distinct op from Opportunity as op 
    inner join op.entity as ent 
    inner join ent.titles as tit 
    with tit.isMain is true
    

    【讨论】:

      【解决方案2】:

      我发现了问题。首先,我必须将实体加入到 Opportunity,然后将标题加入到 Opportunity。所以查询是这样的:

      select opportunity from Opportunity as opportunity 
      join opportunity.entity activeEntity 
        with activeEntity.isActive is true 
      join activeEntity.titles entityTitle 
        with entityTitle.isActive is true
      

      【讨论】:

      • 没错。刚刚添加了一个带有内部连接的版本,您还需要使用distinct。另请注意,问题中没有提到isActive,所以我没有考虑到这一点。
      猜你喜欢
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2014-02-12
      • 2015-12-31
      • 2016-03-02
      • 2021-02-20
      相关资源
      最近更新 更多