Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grails2.0中带有新where查询的参数_Grails_Grails 2.0 - Fatal编程技术网

Grails2.0中带有新where查询的参数

Grails2.0中带有新where查询的参数,grails,grails-2.0,Grails,Grails 2.0,在Grails2.0中定义where查询时是否可以使用参数?例如: def query = Book.where { id == it } Book sub = query.find(5) 我尝试运行该代码,但它在调用find时抛出MissingMethodException。我还尝试在它之前定义一个变量,但它似乎不起作用(因为find返回null,即使我知道它存在) 有什么把戏吗?能够动态更改查询的参数将非常有用 (我知道我可以使用Book.get(5),但为了简单起见,这似乎是最容易

在Grails2.0中定义where查询时是否可以使用参数?例如:

def query = Book.where {
   id == it
}
Book sub = query.find(5)
我尝试运行该代码,但它在调用find时抛出MissingMethodException。我还尝试在它之前定义一个变量,但它似乎不起作用(因为find返回null,即使我知道它存在)

有什么把戏吗?能够动态更改查询的参数将非常有用


(我知道我可以使用Book.get(5),但为了简单起见,这似乎是最容易选择的示例)

这样做的方法似乎是将闭包定义为

import grails.gorm.*

def callable = { id -> 
    id == id
} as DetachedCriteria<Book>

def query = Book.where( callable( id: 5 ) )
导入grails.gorm*
def callable={id->
id==id
}作为分离的标准
def query=Book.where(可调用(id:5))

看起来它应该可以工作,但当我尝试它时,仍然不去(MissingMethodException,抱怨没有eq方法)。还将参数更改为someId,这样它就不会尝试将自身与自身进行比较,并尝试调用(5)(没有映射),但都没有成功。我认为答案是,您必须使用namedquerys来实现这一点。不幸的是,新的where语法太好了!
import grails.gorm.*

def callable = { id -> 
    id == id
} as DetachedCriteria<Book>

def query = Book.where( callable( id: 5 ) )