【问题标题】:Grails combining "in" and "isEmpty" inside and OR ignores "isEmpty"Grails 在内部结合了“in”和“isEmpty”,或者忽略了“isEmpty”
【发布时间】:2015-03-17 12:18:28
【问题描述】:

我希望能够编写一个条件,让我可以搜索所有具有特定 DevelopmentOfInterest(hasMany) 或根本没有的潜在客户。

我的领域模型看起来像这样:

Lead {
   Set<Development> developmentsOfInterest
   static hasMany = [
        developmentsOfInterest:Development
   ]
}

如果我根据开发列表搜索特定列表,我可以在我的条件中使用以下内容:

developmentsOfInterest{
    'in'("id", developments*.id)
}

为了找到所有没有任何进展的人,我使用 isEmpty

isEmpty("developmentsOfInterest")

所以逻辑告诉我,如果我将这两个放在一个 OR 中,我会得到组合列表..

or{
       isEmpty("developmentsOfInterest")
       developmentsOfInterest {
           'in'("id", developments*.id)
       }
 }

它不会..它只会返回感兴趣的列表(基本上忽略了“isEmpty”)

生成的sql如下

where (this_.account_id=? and (not exists (select 1 from lead_development where this_.id=lead_developments_of_interest_id) or (developmen1_.id in (?, ?))))

但我认为这将是正确的 sql 命令?正确读取:

(not exists (select 1 from lead_development where this_.id=lead_developments_of_interest_id) or (developmen1_.id in (?, ?))

更新: 我比较了 isEmpty() 或 sizeEq() 生成的 SQL,以及“in”,“in”添加了一大堆带连接的额外查询代码。 我猜测连接不正确,这导致 isEmpty 不起作用(因为它只会显示与 developmentOfInterests 连接的项目)

where (this_.account_id=? and (? = (select count(*) from lead_development where this_.id=lead_developments_of_interest_id) or this_.id in (?))) order by lower(this_1_.first_name) asc limit ?

Account_id 部分的过滤条件进一步向上。 任何人都可以帮忙吗? 问候,

【问题讨论】:

    标签: hibernate grails


    【解决方案1】:

    你可以把你的标准如下:

    or{
       sizeEq 'developmentsOfInterest', 0
       'in' 'developmentsOfInterest', developments
    }
    

    【讨论】:

    • 嘿,感谢您的回复。如果我使用or{ sizeEq 'developmentsOfInterest', 0 developmentsOfInterest{ 'in'("id", allowedDevelopments*.id) } },它会给我相同的结果,如果我使用sizeEq 'developmentsOfInterest', 0 'in' 'developmentsOfInterest', allowedDevelopments,我会收到一个sql错误没有为参数4指定值。堆栈跟踪如下:消息:没有为参数 4 指定值
    • 看起来你把这个sizeEq 'developmentsOfInterest', 0 'in' 'developmentsOfInterest', allowedDevelopments 放在了一行中:)
    • 这是它生成的 SQL 语句:where (this_.account_id=? and (? = (select count(*) from lead_development where this_.id=lead_developments_of_interest_id) or this_.id in (?))) order by lower(this_1_.first_name) asc limit ? 有没有办法查看正在插入哪些参数?因为显然参数 4 是限制(这很好)所以我猜问题是参数 1-3?就是不知道是哪个
    • 不,那只是我没有在单独的行上进行格式校正:D
    【解决方案2】:

    我知道这是一个非常古老的问题,但我最近遇到了同样的问题,我认为问题是 grails 会默认为内部连接,导致 isEmpty 被忽略

    对我有用的是明确指定连接类型。

    例如:

    Lead.createCriteria().listDistinct {
    createAlias('developmentsOfInterest', 'developmentsOfInterestJoin', JoinType.LEFT_OUTER_JOIN)
    
      or {
          isEmpty('developmentsOfInterest')
          inList('developmentsOfInterestJoin.id', developments)
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 2015-06-30
      • 2020-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-26
      • 2014-09-30
      相关资源
      最近更新 更多