gpt4 book ai didi

grails - 2.0.0.RC1 Where Queries - No such property 异常

转载 作者:行者123 更新时间:2023-12-01 15:19:09 26 4
gpt4 key购买 nike

每次尝试将变量传递给 where 查询时,我都会收到 groovy.lang.MissingPropertyException。

这些是我的域类:

class Book {
String title

static belongsTo = [author: Author]

static constraints = {
title(blank: false, maxSize: 100)
}
}

class Author {
String name

static hasMany = [books: Book]

static constraints = {
name(unique: true, blank: false, maxSize: 50)
}
}

这是引发异常的测试方法:

@Test
void testWhereQuery() {
long authorId = 5
def query = Book.where {
author.id == authorId
}

def books = query.list()
assert books.size() == 0
}

groovy.lang.MissingPropertyException: No such property: authorId for class: grails.gorm.DetachedCriteria
at grails.gorm.DetachedCriteria.methodMissing(DetachedCriteria.groovy:808)
at grails.gorm.DetachedCriteria.build(DetachedCriteria.groovy:723)
at org.grails.datastore.gorm.GormStaticApi.where(GormStaticApi.groovy:116)
at helloworld.BooksIntegrationTests.testWhereQuery(BooksIntegrationTests.groovy:38)

如何将变量传递给查询?

最佳答案

我也一直在与此作斗争。

尝试将您的 where 查询更改为:

def query = Book.where {
author.id.toString() == "${authorId}"
}

这种讨厌的技术只能在字符串内部使用,并且是我能够在 where 查询中使用变量替换的唯一方法。

IMO 缺乏对参数化查询的良好支持,使它们几乎无用。很遗憾,因为符号的格式非常简洁。

关于grails - 2.0.0.RC1 Where Queries - No such property 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8193374/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com