grails - Grails findAll在 'each'闭包内返回空属性

标签 grails groovy

使用findAll获取不带LocationGeoPoint的内容列表。
我必须手动获取每个内容,因为内容(content.location)的属性在每个闭包内为空。这是因为findAll实际上并未从数据库获取吗?

def contents = Content.findAllByLocationGeoPointIsNull()
def location
GeoPoint geoPoint
contents.each { Content content ->
    println content
    if(content.location == null) {
        content = Content.get(content.id)
    }
    location = content.location
    println content.location
    println location
    ...
    content.save()
}

有没有更好的办法?

最佳答案

我可能误解了您的问题,但这是我的建议。您正在请求所有LocationGeoPoint,其中value为null,也许您应该尝试IsNotNull,您可以在此处阅读有关的信息http://gorm.grails.org/latest/hibernate/manual/index.html#finders,可以在此处找到示例。

你可以尝试这样的事情

List<Content> contents = Content.findAllByLocationGeoPointIsNotNull()

contents.each { Content content ->
    // update logic goes here

    content.save()
}

我还建议您查看查询允许您在何处执行批处理更新和删除的地方。

关于grails - Grails findAll在 'each'闭包内返回空属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52069096/

相关文章:

hibernate - Grails在同一事务上分页数据库结果?

unit-testing - 使用rest插件的服务中的Grails模拟方法

Gradle 配置集成

grails - 数据驱动的Spock测试

grails - 尝试使用Grails的Quartz插件时发生编译错误

grails - 在GGTS中构建空白Grails项目时出现构建错误

maven - Grails插件依赖项在应用程序中不可用

groovy - 我可以在 Groovy 中覆盖 cast 运算符吗?

grails - 如何在src/groovy下的类中使用 'classForName'

html - 如何使用 HTTPBuilder 在 Groovy 中设置 GET-Response