我收到一個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()嗎? –