Grails 2.0中带有新查询的参数 [英] Parameters with new where queries in Grails 2.0
问题描述
在Grails 2.0中定义where查询时可以使用参数吗?例如:
def query = Book.where {
id == it
}
Book sub = query.find(5)
我试着运行该代码,但它抛出了MissingMethodException在电话中寻找。我也尝试在它之前定义一个变量,但它似乎并不工作(即使我知道它存在,find也会返回null)。
Long someId = 5
def query = Book.where {
id == someId
}
Book sub = query.find()
任何技巧?能够动态地改变查询的参数将是非常有用的。
(我知道我可以使用Book.get(5),但为了简单起见,这个看起来像是最简单的例子)
似乎要做到这一点的方法是将闭包定义为detachCriteria, p>
import grails.gorm。*
def callable = {id - >
id == id
} as DetachedCriteria< Book>
def query = Book.where(callable(id:5))
Is it possible to use parameters when defining a where query in Grails 2.0? For example:
def query = Book.where {
id == it
}
Book sub = query.find(5)
I tried running that code, but it throws a MissingMethodException on the call to find. I also tried defining a variable before it, but it doesn't seem to work (as find returns null, even though I know it exists).
Long someId = 5
def query = Book.where {
id == someId
}
Book sub = query.find()
Any tricks? Being able to dynamically change the parameters of the query would be extremely useful.
(I know I could just use Book.get(5), but for simplicity sake, this seemed like the easiest example to pick)
Seems like the way to do this is to define a closure as detachCriteria,
import grails.gorm.*
def callable = { id ->
id == id
} as DetachedCriteria<Book>
def query = Book.where( callable( id: 5 ) )
这篇关于Grails 2.0中带有新查询的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!