我有一个实体(作者)和一个呈现所有作者的 Controller 操作。
def index = {
def list = Author.list()
render(view: 'index', model: ['allauthors' : list])
}
渲染页面时,将按预期执行单个查询:
Hibernate:
select
this_.id as id0_0_,
this_.version as version0_0_,
this_.name as name0_0_
from
author this_
但是,当我按“刷新”(F5) 时,将为每个作者执行一个选择语句(这里我有 3 个作者):
Hibernate:
select
author0_.id as id0_0_,
author0_.version as version0_0_,
author0_.name as name0_0_
from
author author0_
where
author0_.id=?
Hibernate:
select
author0_.id as id0_0_,
author0_.version as version0_0_,
author0_.name as name0_0_
from
author author0_
where
author0_.id=?
Hibernate:
select
author0_.id as id0_0_,
author0_.version as version0_0_,
author0_.name as name0_0_
from
author author0_
where
author0_.id=?
为什么会出现这种情况???
最佳答案
看起来这与查询缓存有关。如果你有
cache.use_query_cache = true
在您的 Datasource.groovy 中,但没有在域类中设置缓存,缓存似乎会逐出每个 list()
上的所有条目,并且必须重新缓存每个条目(只是猜测)。
如果您向域添加缓存,则多个选择就会消失 - 事实上,当我在添加此内容后刷新时,不会执行任何选择:
static mapping = {
cache true
}
关于hibernate - 刷新页面时 grails 中奇怪的 GORM 行为 (F5),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10293527/