Grails中findAll,getAll和list之间的区别 [英] Difference between findAll, getAll and list in Grails

查看:779
本文介绍了Grails中findAll,getAll和list之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



查找所有的域类实例:

  Book.findAll()
Book.getAll()
Book.list()

检索指定id的域类的实例:

  Book .findById(1)
Book.get(1)

您什么时候使用每一个?是否有显着的性能差异?

解决方案

getAll 获取,它接受多个ID并返回一个 List 个实例。列表大小将与提供的ID号码相同;任何遗漏将导致该位置的 null 。请参阅 http://grails.org/doc/latest/ref/Domain%20Classes/getAll.html



findAll 允许您使用HQL查询并支持分页,但它们不限于实例的调用类,所以我使用 executeQuery 代替。请参阅 http://grails.org/doc/latest/ref/Domain%20Classes/findAll.html



list 查找所有实例并支持分页。请参阅 http://grails.org/doc/latest/ref/Domain%20Classes/list.html



get 通过id检索单个实例。它使用 instance 缓存,因此在同一Hibernate会话中多次调用最多只会导致一次数据库调用(例如,如果实例位于第二级缓存中并且已启用它) / p>

findById 是一个动态查找器,如 findByName findByFoo 等等。因此它不使用实例缓存,但是如果启用了查询缓存(通常不是一个好主意),它可以被缓存。 get 应该是首选,因为它的缓存更加智能;缓存的查询结果(即使对于像这样的单个实例)被悲观地清除的次数比您预期的要多,但实例缓存并不需要如此悲观。



对于 findById 来说,一个用例就是与另一个属性结合使用的安全相关检查。例如,我不会使用 CreditCard.get(cardId)检索 CreditCard 实例,在用户中使用 CreditCard.findByIdAndUser(cardId,user)。这假定 CreditCard 具有用户用户属性。这样两个属性必须匹配,并且这会阻止黑客访问卡实例,因为卡ID可能匹配,但用户不会。


With Grails there are several ways to do the same thing.

Finds all of domain class instances:

Book.findAll()
Book.getAll()
Book.list()

Retrieves an instance of the domain class for the specified id:

Book.findById(1)
Book.get(1)

When do you use each one? Are there significant differences in performance?

解决方案

getAll is an enhanced version of get that takes multiple ids and returns a List of instances. The list size will be the same as the number of provided ids; any misses will result in a null at that slot. See http://grails.org/doc/latest/ref/Domain%20Classes/getAll.html

findAll lets you use HQL queries and supports pagination, but they're not limited to instances of the calling class so I use executeQuery instead. See http://grails.org/doc/latest/ref/Domain%20Classes/findAll.html

list finds all instances and supports pagination. See http://grails.org/doc/latest/ref/Domain%20Classes/list.html

get retrieves a single instance by id. It uses the instance cache, so multiple calls within the same Hibernate session will result in at most one database call (e.g. if the instance is in the 2nd-level cache and you've enabled it).

findById is a dynamic finder, like findByName, findByFoo, etc. As such it does not use the instance cache, but can be cached if you have query caching enabled (typically not a good idea). get should be preferred since its caching is a lot smarter; cached query results (even for a single instance like this) are pessimistically cleared more often than you would expect, but the instance cache doesn't need to be so pessimistic.

The one use case I would have for findById is as a security-related check, combined with another property. For example instead of retrieving a CreditCard instance using CreditCard.get(cardId), I'd find the currently logged-in user and use CreditCard.findByIdAndUser(cardId, user). This assumes that CreditCard has a User user property. That way both properties have to match, and this would block a hacker from accessing the card instance since the card id might match, but the user wouldn't.

这篇关于Grails中findAll,getAll和list之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆