【问题标题】:NamedQuery not returning result when property is null?当属性为空时,NamedQuery 不返回结果?
【发布时间】:2012-11-13 15:26:11
【问题描述】:

您好,我在构建命名查询时似乎碰壁了。

我有以下情况: 一个人有一个可选的地址,一个地址有一个可选的国家。

现在我想过滤一堆字段并返回结果 搜索一些文本。

我尝试了以下部分,但是每当一个人有一个位置和地址并且地址没有国家时,我就没有结果。

    filterOnAddress { String loc ->
        if(!loc.isEmpty()) {
            location {
                address {
                    or {
                        ilike 'street', "%${loc}%"
                        ilike 'city',  "%${loc}%"
                        country {
                            or {
                                ilike 'titleLocal',  "%${loc}%"
                                ilike 'title',  "%${loc}%"
                            }
                        }
                    }
                }
            }
        }
    }
}

filterOnAddress { String loc ->
    if(!loc.isEmpty()) {
        location {
            address {
                or {
                    ilike 'street', "%${loc}%"
                    ilike 'city',  "%${loc}%"
                    and {
                        isNotNull('country')
                        country {
                            or {
                                ilike 'titleLocal',  "%${loc}%"
                                ilike 'title',  "%${loc}%"
                            }
                        }

                    }
                }
            }
        }
    }
}

只要 Person 的 Country 为空,查询就不会返回 据我所知,一些标准的预期结果是 遇见了!因此,当一个国家/地区为空时,我会期待一些结果。

当国家/地区为空但其他国家/地区为空时,我需要在查询中包含什么 标准是否成功?

【问题讨论】:

  • 你能为那个场景构建一个简单的 SQL 查询吗?也许这是一个很好的起点。

标签: grails grails-orm


【解决方案1】:

通过快速扫描,这对我来说看起来很合法。我建议删除国家部分并确保您在没有它的情况下获取数据。然后我的下一步是打开 sql 日志记录(将 logSql 元素添加到数据源,根据 http://grails.org/doc/latest/guide/conf.html#dataSource),并查看用于查找数据的内容。

【讨论】:

    猜你喜欢
    • 2015-08-14
    • 2020-10-26
    • 2021-05-23
    • 1970-01-01
    • 2018-01-05
    • 2020-12-30
    • 2016-04-11
    • 1970-01-01
    • 2019-07-09
    相关资源
    最近更新 更多