我收到一个groovy.lang.MissingPropertyException每次我试图传递一个变量到一个where查询。2.0.0.RC1凡查询 - 没有这样的属性异常
这些都是我的领域类:
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)
我如何传递一个变量来查询?
我解决了某种方式将域对象作为变量传递。如果条件变成'author == _author',其中_author就像'def _author = Author.get(authorId)'一样工作。 –
Author.get是一个动态注入方法,不使用where查询。您也可以使用.findBy ...,.findAll ...,.count等等。 – Todd
对不起,我的意思是'def query = Book.where {author = Author.get(authorId)}'。你认为这会推迟执行直到调用list()吗? –