刷新页面时Grails中出现奇怪的GORM行为(F5)

时间:2012-04-24 07:26:01

标签: hibernate grails gorm

我有一个实体(作者)和一个呈现所有作者的控制器动作。

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_

然而,当我按下Refresh(F5)然后为每个作者执行一个select语句(这里我有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=?

为什么会发生这种情况?

1 个答案:

答案 0 :(得分:1)

看起来这与查询缓存有关。如果你有

cache.use_query_cache = true

在您的Datasource.groovy中,但没有在您的域类中设置缓存,缓存似乎驱逐了每个list()上的所有条目,并且必须重新缓存每个条目(只是猜测)。

如果你向你的域添加缓存,那么多个选择就会消失 - 事实上,在我添加这个选项后刷新时会完成NO选择:

static mapping = {
    cache true
}