Fetching sets of objects from GORM
I'm working on Grails. At some point I have to fetch large sets of objects from the db, based on their ids. If I do this id by id, the performance is very bad.
ids.each{
Myclass.findById( id )
...
}
Given that for storing batches there's the useful withTransaction closure, is it possible to do som开发者_高级运维ething like that to fetch objects, instead of storing them?
Another idea could be a long HQL query like:
"select * from Myclass where (id = 1) OR ( id = 2) ... OR ( id = n )"
Is this an ok solution?
Thanks!
As noted in this question, HQL supports a better syntax for selecting based on a group of values, which is supported by GORM's find()
method. GORM dynamic finders would also support syntax like:
def myObjs = MyClass.findByIdInList(ids)
Similarly, if your operation is read-only, you could use getAll()
, which will take advantage of the optimizations Hibernate can make for read-only transations.
精彩评论