从 GORM 获取对象集
我正在研究 Grails。 在某些时候,我必须根据对象的 ID 从数据库中获取大量对象。 如果我逐个id执行此操作,性能非常糟糕。
ids.each{
Myclass.findById( id )
...
}
鉴于存储批次有有用的 withTransaction 闭包,是否可以执行类似的操作来获取对象,而不是存储它们?
另一个想法可能是一个很长的 HQL 查询,例如:
"select * from Myclass where (id = 1) OR ( id = 2) ... OR ( id = n )"
这是一个好的解决方案吗?
谢谢!
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 something 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如这个问题中所述,HQL 支持更好的语法基于一组值进行选择,GORM 的
find()
方法支持这一点。 GORM 动态查找器 也支持类似语法:同样,如果您的操作是只读的,您可以使用 getAll(),它将利用 Hibernate 对只读事务的优化。
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: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.