Options with a new one where requests are in Grails 2.0

Can parameters be used when defining a where query in Grails 2.0? For instance:

def query = Book.where { id == it } Book sub = query.find(5) 

I tried running this code, but it throws a MissingMethodException to find. I also tried to define the variable in front of it, but it does not work (since find returns null, although I know that it exists).

 Long someId = 5 def query = Book.where { id == someId } Book sub = query.find() 

Any tricks? The ability to dynamically change query parameters will be extremely useful.

(I know I can just use Book.get (5), but for simplicity, it looked like the simplest example)

+4
source share
1 answer

It seems like a way to do this is to define the closure as detachCriteria,

 import grails.gorm.* def callable = { id -> id == id } as DetachedCriteria<Book> def query = Book.where( callable( id: 5 ) ) 
-1
source

Source: https://habr.com/ru/post/1379778/


All Articles