5

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

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=?

为什么会出现这种情况???

4

1 回答 1

1

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

cache.use_query_cache = true

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

如果您将缓存添加到您的域,则多个选择会消失 - 实际上,当我在添加此内容后刷新时不会完成任何选择:

static mapping = {
    cache true
}
于 2012-07-14T23:51:40.713 回答